JS實現(xiàn)獲取當(dāng)前URL和來源URL的方法
本文實例講述了JS實現(xiàn)獲取當(dāng)前URL和來源URL的方法。分享給大家供大家參考,具體如下:
index.html:
<!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1, user-scalable=no"> <title>新建H5模板</title> </head> <body> <a href="demo.html">鏈接</a> </body> </html>
demo.html:
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1, user-scalable=no">
<title>新建H5模板</title>
</head>
<body>
當(dāng)前URL:<input type="text" style=" width:300px;" name="nowurl" id="nowurl"><br>
來源URL:<input type="text" style=" width:300px;" name="fromurl" id="fromurl">
<script>
var nowurl = document.URL;
var fromurl = document.referrer;
document.getElementById('nowurl').value = nowurl;
document.getElementById('fromurl').value = fromurl;
</script>
</body>
</html>
效果圖:
假設(shè)是通過 http://www.dhdzp.com/index.html 進來

那么:
獲取當(dāng)前的URL是:http://www.dhdzp.com/demo.html
獲取來源的URL是:http://www.dhdzp.com/index.html

說明:
document.URL 屬性可返回當(dāng)前文檔的 URL。
document.referrer 屬性可返回載入當(dāng)前文檔的文檔的 URL。
更多關(guān)于JavaScript相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《JavaScript查找算法技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》、《JavaScript中json操作技巧總結(jié)》、《JavaScript切換特效與技巧總結(jié)》、《JavaScript動畫特效與技巧匯總》、《JavaScript錯誤與調(diào)試技巧總結(jié)》及《JavaScript數(shù)學(xué)運算用法總結(jié)》
希望本文所述對大家JavaScript程序設(shè)計有所幫助。
相關(guān)文章
使用JavaScript實現(xiàn)一個靜態(tài)鏈表
使用js判斷數(shù)組中是否包含某一元素(類似于php中的in_array())

