本文目錄一覽:
兩種js獲取當前域名代碼
今天給各位朋友介紹兩種js獲取當前域名
代碼如下
//獲取當前域名
1、window.location.host;
2、document.domain;
//獲取當前頁面地址
url
=
window.location.href;
例子
代碼如下
script
language=”javascript”
//獲取域名
host
=
window.location.host;
host2=document.domain;
//獲取頁面完整地址
url
=
window.location.href;
document.write(“brhost=”+host)
document.write(“brhost2=”+host2)
document.write(“brurl=”+ur()l)
/script
補充:
獲取當前域名信息
代碼如下
thisTLoc
=
top.location.href;
thisPLoc
=
parent.document.location;
thisTHost
=
top.location.hostname;
thisHost
=
location.hostname;
strwrite
=
”
thisTLoc:
[“
+
thisTLoc
+
“]”
strwrite
+=
”
thisPLoc:
[“
+
thisPLoc
+
“]”
strwrite
+=
”
thisTHost:
[“
+
thisTHost
+
“]”
strwrite
+=
”
thisHost:
[“
+
thisHost
+
“]”
document.write(
strwrite
);
用JS獲取當前域名並判斷
在頁面插入js代碼
script language=”javascript” type=”text/javascript”
function loadFN(){
hrefValue = window.location.href; //獲取當前頁面的地址
alertUrls = [”]; //指定你想要alert的域名,多個可以在數組中直接追加即可
//追加如:[”, ‘bbb.com’, ‘abc.bbb.com’]
for(key in alertUrls){
if(String(hrefValue).indexOf(alertUrls[key]) = 0){
//指定的字符串值alertUrls[key]在字符串href中出現則=0,否則為-1
alert( alertUrls[key] );
break; //直接跳出循環
}
}
}
///頁面載入完成後即執行loadFN函數
window.onload = loadFN;//前提是原來頁面沒有使用onload,否則還要做些針對的處理
/script
window.onload = loadFN;//前提是原來頁面沒有使用onload,否則還要做些針對的處理
如何使用Javascript獲取一個鏈接地址中的頂級域名
a id=”a1″ href=”” target=”_blank”JS特效/a
script language=”javascript”
alert(getdomain(1,’a1′)); //彈窗輸出域名
function getdomain(typ,id){ //參數:類型(0:當前域名,1:頂級域名),鏈接對象ID
var url=window.document.getElementById(id).href; //獲取鏈接
if(typ==0){ //獲取當前域名,如:
var a = document.createElement(‘a’);
a.href = url;
url=a.hostname;
}else{ //獲取頂級域名,如:lingchenliang.com
//使用正則表達式
url=url.replace(/.+[\.\/]([A-z]+\.[A-z]+)\/[^\/].+/,”$1″);
}
return url; //返回域名值
}
/script
如何通過js獲取當前訪問頁面的域名
input type=”text” style=” width:300px;” name=”new” id=new”
script
var nurl = document.referrer;//來源url
document.URL //獲取當前域名
document.title//獲取當前頁面標題
document.getElementById(‘new’).innerHTML = nurl;/script
我是愛分享資源網的站長,如果你覺得不錯請訪問下我的網站,謝謝了!
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/158305.html