導航:首頁 > 編程語言 > js獲取自身路徑

js獲取自身路徑

發布時間:2021-03-23 17:41:54

㈠ 如何在HTML文件中通過javascript獲得當前網頁的絕對路徑和相對路徑

絕對路徑:

<!DOCTYPEhtml>
<html>
<body>

<script>

document.write(location.href);//輸出當前頁面完整URL

</script>

</body>
</html>

相對路回徑:

<!DOCTYPEhtml>
<html>
<body>

<script>

document.write(location.pathname);//返回當前URL的路答徑名

</script>

</body>
</html>

js取得本地路徑的代碼

使用javascript來獲取當前url網址非常有用。下面以例子講解:

輸入的網址是(沒有框架):http://localhost:81/Test/1.htm?Did=123
<br>以下為輸出:
<br>
<SCRIPT>

//獲取Url傳過來的值
function Request(name)
{
new RegExp("(^|&)"+name+"=([^&]*)").exec(window.location.search.substr(1));
return RegExp.$2
}

thisURL = document.URL; // http://localhost:81/Test/1.htm?Did=123
thisHREF = document.location.href; // http://localhost:81/Test/1.htm?Did=123
thisSLoc = self.location.href; // http://localhost:81/Test/1.htm?Did=123
thisDLoc = document.location; // http://localhost:81/Test/1.htm?Did=123

thisTLoc = top.location.href; // http://localhost:81/Test/1.htm?Did=123
thisPLoc = parent.document.location;// http://localhost:81/Test/1.htm?Did=123
thisTHost = top.location.hostname; // localhost
thisHost = location.hostname; // localhost

thisU1 = window.location.protocol; // http:
thisU2 = window.location.host; // localhost:81
thisU3 = window.location.pathname; // /Test/1.htm

document.writeln( thisURL + "<br />");
document.writeln( thisHREF + "<br />");
document.writeln( thisSLoc + "<br />");
document.writeln( thisDLoc + "<br />");

document.writeln( thisTLoc + "<br />");
document.writeln( thisPLoc + "<br />");
document.writeln( thisTHost + "<br />");
document.writeln( thisHost + "<br />");

document.writeln( thisU1 + "<br />");
document.writeln( thisU2 + "<br />");
document.writeln( thisU3 + "<br />");

document.writeln( "Did="+Request("Did") );// Did=123
</SCRIPT>

㈢ js獲取文件路徑怎麼寫

<script>
$("input[name=fileString]").change(function(){
//varnames=[];

for(vari=0;i<$(this).get(0).files.length;++i){
//names.push($(this).get(0).files[i].name);
//console.log($(this).get(0).files[i].mozFullPath);
//方式一:
varfilePath=$(this).val();
console.log(filePath);
//方式二:
alert($('input[type=file]').val());
}
//console.log(names);
//方式三:
alert($("input[name=fileString]").val());

})

</script>

注意:以上方式取到的都是文件的偽路徑,由於瀏覽器的安全策略,Chrome瀏覽器及內核的瀏覽器是不可能獲取文件在本地的真實路徑的。IE瀏覽器可以通過設置安全級別,能獲取到真實路徑。

㈣ js如何獲取當前頁面所在的路徑

可以使用window對象的location對象的里的href來獲取當前頁面的URL地址。

工具原料:編輯器、瀏覽器

1、Location 對象包含有關當前 URL 的信息,Location 對象是 Window 對象的一個部分,可通過 window.location 屬性來訪問,代碼示例如下:

<html>
<head><scripttype="text/javascript">
<script>
alert(window.location.href);
</script>
</script>
</head>
<body>
test
</body>
</html>

2、運行的結果如下圖:

㈤ 如何用js獲取當前打開的頁面的路徑

設置或獲取對象指定的文件名或路徑。
alert(window.location.pathname)

設置或獲取整個 URL 為字元串。內

alert(window.location.href);
設置或獲取與 URL 關聯的埠號容碼。
alert(window.location.port)

設置或獲取 URL 的協議部分。
alert(window.location.protocol)

設置或獲取 href 屬性中在井號「#」後面的分段。
alert(window.location.hash)

設置或獲取 location 或 URL 的 hostname 和 port 號碼。
alert(window.location.host)

設置或獲取 href 屬性中跟在問號後面的部分。
alert(window.location.search)

㈥ js中如何獲取當前瀏覽器的地址的值

js中通過window.location.href和document.location.href、document.URL獲取當前瀏覽器的地址的值,它們的的區別是:
1、document表示的是一個文檔對象專,window表示的是一個窗口對屬象,一個窗口下可以有多個文檔對象。所以一個窗口下只有一個window.location.href,但是可能有多個document.URL、document.location.href
2、window.location.href和document.location.href可以被賦值,然後跳轉到其它頁面,document.URL只能讀不能寫
3、document.location.href和document.location.replace都可以實現從A頁面切換到B頁面,但他們的區別是:
用document.location.href切換後,可以退回到原頁面。
而用document.location.replace切換後,不可以通過「後退」退回到原頁面。

㈦ js 如何獲取當前網頁的本地路徑

varlcal=location.href;
lcal=substring(0,lcal.lastIndexOf(""));

㈧ js 得到保存路徑

classloader

㈨ 如何利用JS獲取到的完整路徑

js,可以使用如下獲取
window.location.href
一般來說,路徑都使用jsp的內置對象獲取,比較基礎,網上一搜一大堆

㈩ js 獲取上級目錄路徑

//假設你的路徑為str
str="a.com/aaa/bbb/1.html"
str=str.replace(/\/[^\/]+\/[^\/]+$/,"/");

閱讀全文

與js獲取自身路徑相關的資料

熱點內容
cocoapods版本更新 瀏覽:361
國外it大神教你學習如何編程 瀏覽:312
手機存儲應用程序 瀏覽:284
頁面自適應屏幕如何調整代碼數據 瀏覽:681
jsjson工具 瀏覽:299
資料庫中如何備份一張表的數據 瀏覽:739
網路設備能用到什麼 瀏覽:64
暴風轉碼如何添加文件夾 瀏覽:515
延安整合網路營銷有哪些 瀏覽:74
查找word打開過的文件在哪裡 瀏覽:137
b樹java代碼 瀏覽:683
電腦文件存儲 瀏覽:657
蘭州中考徵集志願在哪個網站 瀏覽:215
cs文件上傳下載 瀏覽:244
拷貝文件到根目錄下重命名linux 瀏覽:603
api函數的頭文件 瀏覽:249
華為怎麼綁定迷你編程 瀏覽:215
機構怎麼申請少兒編程考級 瀏覽:495
崑山數控編程哪裡好學 瀏覽:459
jspcfor跳出 瀏覽:65

友情鏈接