- 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;
- }
- alert(getHost());
- alert(getHost('https://test.com'));
- 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 + "]<br />"</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 + "]<br />"</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 + "]<br />"</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 + "]<br />"</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)"></script></span></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><script type="text/javascript"></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 + "]<br />"<wbr><br> strwrite += " thisPLoc:<wbr>[" + thisPLoc + "]<br />"<wbr><br> strwrite += " thisTHost:<wbr>[" + thisTHost + "]<br />"<wbr><br> strwrite += " thisHost:<wbr>[" + thisHost + "]<br />"<wbr><br> document.write( strwrite );<wbr><wbr><br><wbr><wbr><wbr></script></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><script type="text/javascript"><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 + "]<br />"<wbr><br> strwrite += " thisUPage:<wbr>[" + thisUPage + "]<br />"<wbr><br> document.write( strwrite );<wbr><br><wbr><wbr><wbr></script></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
搜索标签:获取当前域名 | ||
js获取当前域名 | ||
[阅读次数:866次][发布时间:2010年12月12日] | ||
<script> <script> 还有一种稍有些复杂的取域名的方法,也是过滤了文件夹名,文件名,参数…… var getHost = function(url) |
//以下是函数的写法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
}