两种js获取当前域名代码

JavaScript015

两种js获取当前域名代码,第1张

今天给各位朋友介绍两种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("<br>host="+host)

document.write("<br>host2="+host2)

document.write("<br>url="+ur(www.45it.net)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代码

<script language="javascript" type="text/javascript">

function loadFN(){

hrefValue = window.location.href//获取当前页面的地址

alertUrls = ['www.bbb.com'] //指定你想要alert的域名,多个可以在数组中直接追加即可

//追加如:['www.bbb.com', '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,否则还要做些针对的处理

js域名授权还是不现实的,js运行在客户端,浏览器可以禁用js

你应该在服务器端进行域名授权还好些

况且,js存在浏览器兼容问题,在每种浏览器、每种版本的浏览器下,对js的解析都有差异