[JavaScript] 用js如何获取当前url地址中的域名

本文介绍了一种使用JavaScript获取当前网页域名的方法,并提供了多个示例代码片段。通过正则表达式匹配URL来提取域名部分,适用于不同场景下的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. var getHost = function(url) {
  2. var host = "null";
  3. if(typeof url == "undefined"
  4. || null == url)
  5. url = window.location.href;
  6. var regex = /.*\:\/\/([^\/]*).*/;
  7. var match = url.match(regex);
  8. if(typeof match != "undefined"
  9. && null != match)
  10. host = match[1];
  11. return host;
  12. }
  13. alert(getHost());
  14. alert(getHost('https://test.com'));
  15. alert(getHost('http://test.com/123.html'));

复制代码


<script type="text/javascript"><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; background-color:rgb(236,249,238)"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; background-color:rgb(236,249,238)"><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; background-color:rgb(236,249,238)"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:20px; background-color:rgb(236,249,238)"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; background-color:rgb(236,249,238)"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:20px; background-color:rgb(236,249,238)"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; background-color:rgb(236,249,238)"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; background-color:rgb(236,249,238)"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:20px; background-color:rgb(236,249,238)">thisURL = document.URL;</span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; background-color:rgb(236,249,238)"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; background-color:rgb(236,249,238)"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:20px; background-color:rgb(236,249,238)">thisHREF = document.location.href;</span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; background-color:rgb(236,249,238)"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; background-color:rgb(236,249,238)"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:20px; background-color:rgb(236,249,238)">thisSLoc = self.location.href;</span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; background-color:rgb(236,249,238)"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; background-color:rgb(236,249,238)"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:20px; background-color:rgb(236,249,238)">thisDLoc = document.location;</span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; background-color:rgb(236,249,238)"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; background-color:rgb(236,249,238)"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:20px; background-color:rgb(236,249,238)">strwrite = " thisURL:</span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; background-color:rgb(236,249,238)"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:20px; background-color:rgb(236,249,238)">[" + thisURL + "]&lt;br /&gt;"</span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; background-color:rgb(236,249,238)"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; background-color:rgb(236,249,238)"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:20px; background-color:rgb(236,249,238)">strwrite += " thisHREF:</span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; background-color:rgb(236,249,238)"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:20px; background-color:rgb(236,249,238)">[" + thisHREF + "]&lt;br /&gt;"</span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; background-color:rgb(236,249,238)"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; background-color:rgb(236,249,238)"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:20px; background-color:rgb(236,249,238)">strwrite += " thisSLoc:</span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; background-color:rgb(236,249,238)"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:20px; background-color:rgb(236,249,238)">[" + thisSLoc + "]&lt;br /&gt;"</span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; background-color:rgb(236,249,238)"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; background-color:rgb(236,249,238)"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:20px; background-color:rgb(236,249,238)">strwrite += " thisDLoc:</span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; background-color:rgb(236,249,238)"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:20px; background-color:rgb(236,249,238)">[" + thisDLoc + "]&lt;br /&gt;"</span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; background-color:rgb(236,249,238)"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; background-color:rgb(236,249,238)"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:20px; background-color:rgb(236,249,238)">document.write( strwrite );</span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; background-color:rgb(236,249,238)"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; background-color:rgb(236,249,238)"><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; background-color:rgb(236,249,238)"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:20px; background-color:rgb(236,249,238)"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; background-color:rgb(236,249,238)"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:20px; background-color:rgb(236,249,238)"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; background-color:rgb(236,249,238)"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:20px; background-color:rgb(236,249,238)">&lt;/script&gt;</span></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr><wbr>&lt;script type="text/javascript"&gt;</wbr></wbr></wbr>

thisTLoc = top.location.href;<wbr><br> thisPLoc = parent.document.location;<wbr><br> thisTHost = top.location.hostname;<wbr><br> thisHost = location.hostname;<wbr><br> strwrite = "<wbr>thisTLoc:<wbr>[" + thisTLoc + "]&lt;br /&gt;"<wbr><br> strwrite += " thisPLoc:<wbr>[" + thisPLoc + "]&lt;br /&gt;"<wbr><br> strwrite += " thisTHost:<wbr>[" + thisTHost + "]&lt;br /&gt;"<wbr><br> strwrite += " thisHost:<wbr>[" + thisHost + "]&lt;br /&gt;"<wbr><br> document.write( strwrite );<wbr><wbr><br><wbr><wbr><wbr>&lt;/script&gt;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr><wbr>&lt;script type="text/javascript"&gt;<wbr><br><wbr><br> tmpHPage = thisHREF.split( "/" );<wbr><br> thisHPage = tmpHPage[ tmpHPage.length-1 ];<wbr><br> tmpUPage = thisURL.split( "/" );<wbr><br> thisUPage = tmpUPage[ tmpUPage.length-1 ];<wbr><br> strwrite = " thisHPage:<wbr>[" + thisHPage + "]&lt;br /&gt;"<wbr><br> strwrite += " thisUPage:<wbr>[" + thisUPage + "]&lt;br /&gt;"<wbr><br> document.write( strwrite );<wbr><br><wbr><wbr><wbr>&lt;/script&gt;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>




搜索标签:获取当前域名
js获取当前域名
[阅读次数:866次][发布时间:2010年12月12日]

<script>
alert(location.hostname)
alert(location.port)
alert(location.pathname)
alert(location.search)
</script>

<script>
function winsx(url) //URL传递的参数
{
var winurl =url?url:(unescape(window.location.href));
var wlp = winurl.split("?")[1];
var wincs = wlp.split("&");
for(var i=0; i<wincs.length; i++)
{
var tur = wincs[i].split("=");
eval('this.'+tur[0]+'="'+tur[1]+'";');
}
}
var ug = new winsx("http://www.yourname.com/aa/bb.php?val=11&test=2");
alert(ug.val)
alert(ug.test)
</script>

还有一种稍有些复杂的取域名的方法,也是过滤了文件夹名,文件名,参数……

var getHost = function(url)
{
var host = "null";
if(typeof url == "undefined"|| null == url)
{
url = window.location.href;
}
var regex = /.*\:\/\/([^\/]*).*/;
var match = url.match(regex);
if(typeof match != "undefined" && null != match)
{
host = match[1];
}
return host;
}




//以下是函数的写法
functionGetParam(){
varurl=document.location.href;
varname=""
if(url.indexOf("=")>0)
{
name
=url.substring(url.indexOf("=")+1,url.length)
}

returnname;
}



/*
获取指定的URL参数值
URL:http://www.blogjava.net/blog?name=bainian
参数:paramNameURL参数
调用方法:getParam("name")
返回值:bainian
*/

//1.
functiongetParam(paramName)
{
paramValue
="";
isFound
=false;
if(this.location.search.indexOf("?")==0&&this.location.search.indexOf("=")>1)
{
arrSource
=unescape(this.location.search).substring(1,this.location.search.length).split("&");
i
=0;
while(i<arrSource.length&&!isFound)
{
if(arrSource[i].indexOf("=")>0)
{
if(arrSource[i].split("=")[0].toLowerCase()==paramName.toLowerCase())
{
paramValue
=arrSource[i].split("=")[1];
isFound
=true;
}

}

i
++;
}

}

returnparamValue;
}



//2.
functionRequest(sName)
{

/*
getlastloc.of?
right:findfirstloc.ofsName
+2
retrievevaluebeforenext&

*/


varsURL=newString(window.location);
varsURL=document.location.href;
variQMark=sURL.lastIndexOf('?');
variLensName=sName.length;

//retrieveloc.ofsName
variStart=sURL.indexOf('?'+sName+'=')//limitation1
if(iStart==-1)
{//notfoundatstart
iStart=sURL.indexOf('&'+sName+'=')//limitation1
if(iStart==-1)
{//notfoundatend
return0;//notfound
}

}


iStart
=iStart++iLensName+2;
variTemp=sURL.indexOf('&',iStart);//nextpairstart
if(iTemp==-1)
{//EOF
iTemp=sURL.length;
}

returnsURL.slice(iStart,iTemp);
sURL
=null;//destroyString
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值