javascript 獲取當前頁面的URL


如圖所示:點擊錄入按鈕實際的請求路徑是“http://localhost:8081/dodiscovery/ordertaoziController.do?goAdd&_=1622601413035”

接下來測試js腳本獲取url路徑中的不同信息

1、獲取完整URL

var url;
url = window.location.href;

 

 結果是:http://localhost:8081/dodiscovery/ordertaoziController.do?goAdd&_=1622601413035

2、獲取文件路徑(文件地址)

url = window.location.pathname;

 

 結果是:/dodiscovery/ordertaoziController.do

3、獲取屬性(“?”后面的分段)

url = window.location.search;

 

結果是:?goAdd&_=1622601413035

4、獲取協議

url = window.location.protocol;

 

結果是:http:

5、獲取主機地址和端口號

url = window.location.host;

 

結果是:localhost:8081

6、獲取主機地址

url = window.location.hostname;

 

結果是:localhost

7、獲取端口號

url = window.location.port;

 

結果是:8081

8、獲取錨點(“#”后面的分段)

url = window.location.hash;

 

結果是:空(當前路徑下沒有錨點)

9、獲取當前地址欄中顯示的URL

url = window.parent.location.href;
或者
url = parent.window.location.href;(window parent 可互換)

 

結果是:http://localhost:8081/dodiscovery/loginController.do?login#

10、如果頁面中使用了框架(frameset)要獲取到指定頁面的URL,只要把window換成指定的頁面即可

url = window.parent.frames['frame'].location.href;('frame'為指定頁面的class名)

結果是:語句錯誤,原因是我沒找到具體使用了什么框架,或class沒找到,總之這句不會使用

 

 11、從URL中獲取指定參數

url = new URL("http://localhost:8080/dodiscovery/dataVisualConfigController.do?view&dvCode=cockpit&cell_code=cell01&clickFunctionId=402881a67f483bd6017f48445a38000d");
url.searchParams.get("cell_code");

 

以上除了第十條其他都是自己一一測試過的。

原文鏈接:https://blog.csdn.net/u013288800/article/details/82787641

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM