導航:首頁 > 編程語言 > 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獲取自身路徑相關的資料

熱點內容
ios判斷沙盒文件是否存在 瀏覽:681
知識瀏覽app 瀏覽:640
ug編程外r角怎麼弄出來 瀏覽:474
點擊展開隱藏代碼 瀏覽:76
華強北組裝機蘋果手機 瀏覽:683
直接在工件上扎溝怎麼編程對刀 瀏覽:145
智能車教程 瀏覽:615
大數據干到多少歲 瀏覽:508
excel2013的工作薄文件默認擴展名 瀏覽:609
iphone6splus水貨報價 瀏覽:12
單向鏈表如何存入數據 瀏覽:312
視頻播客程序 瀏覽:25
有什麼做文件壓縮包免費的軟體 瀏覽:658
問卷星下拉題目為什麼數據是序號 瀏覽:161
蘇寧銷售數據多少 瀏覽:412
MACpro顯示器顏色的描述文件 瀏覽:598
如何算一組數據平均值 瀏覽:741
手機臨時緩存文件有哪些 瀏覽:186
如何關閉數據漫遊 瀏覽:120
怎麼改appstore語言 瀏覽:776

友情鏈接