两种js获取当前域名代码

JavaScript010

两种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中window对象的location子对象的pathname属性,返回url所对应的网页文件的虚拟路径,其中包括网页文件的文件名。用法如下:

var str = window.location.pathname

在你举的例子中少了个端口号应该http://hostname:port/index.html

这时str 的值就为 /index.html

如果目录更深如http://hostname:port/wtccut/index.html

那么str 的值就为 /wtccut/index.html

str为网页文件的虚拟路径,如果不是你要的值你可以通过正则表达式继续截取。

你可以用

var str = window.location.hostname 获得主机名或ip地址

var str = window.location.port 获得端口号

<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>

我是爱分享资源网的站长,如果你觉得不错请访问下我的网站,谢谢了!