[JavaScript] 用js如何获取当前url地址中的域名
来源:互联网 发布:广元广电网络宽带资费 编辑:程序博客网 时间:2024/06/05 02:17
- 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">
thisURL = document.URL;
thisHREF = document.location.href;
thisSLoc = self.location.href;
thisDLoc = document.location;
strwrite = " thisURL:
strwrite += " thisHREF:
strwrite += " thisSLoc:
strwrite += " thisDLoc:
document.write( strwrite );
thisTLoc = top.location.href;
thisPLoc = parent.document.location;
thisTHost = top.location.hostname;
thisHost = location.hostname;
strwrite = "
strwrite += " thisPLoc:
strwrite += " thisTHost:
strwrite += " thisHost:
document.write( strwrite );
tmpHPage = thisHREF.split( "/" );
thisHPage = tmpHPage[ tmpHPage.length-1 ];
tmpUPage = thisURL.split( "/" );
thisUPage = tmpUPage[ tmpUPage.length-1 ];
strwrite = " thisHPage:
strwrite += " thisUPage:
document.write( strwrite );
搜索标签: 获取 当前域名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;
}
//以下是函数的写法
function GetParam(){
var url = document.location.href;
var name=""
if (url.indexOf("=")>0)
{
name = url.substring(url.indexOf("=")+1,url.length)
}
return name;
}
/*
获取指定的URL参数值
URL:http://www.blogjava.net/blog?name=bainian
参数:paramName URL参数
调用方法:getParam("name")
返回值:bainian
*/
//1.
function getParam(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++;
}
}
return paramValue;
}
//2.
function Request(sName)
{
/*
get last loc. of ?
right: find first loc. of sName
+2
retrieve value before next &
*/
var sURL = new String(window.location);
var sURL = document.location.href;
var iQMark= sURL.lastIndexOf('?');
var iLensName=sName.length;
//retrieve loc. of sName
var iStart = sURL.indexOf('?' + sName +'=') //limitation 1
if (iStart==-1)
{//not found at start
iStart = sURL.indexOf('&' + sName +'=')//limitation 1
if (iStart==-1)
{//not found at end
return 0; //not found
}
}
iStart = iStart + + iLensName + 2;
var iTemp= sURL.indexOf('&',iStart); //next pair start
if (iTemp ==-1)
{//EOF
iTemp=sURL.length;
}
return sURL.slice(iStart,iTemp ) ;
sURL=null;//destroy String
}
- [JavaScript] 用js如何获取当前url地址中的域名
- JS获取当前网址,JS获取当前域名URL ,JS获取网站完整路径页面地址
- js取得当前url,javascript获取当前页面url值,js获取域名
- js取得当前url,javascript获取当前页面url值,js获取域名
- js取得当前url,javascript获取当前页面url值,js获取域名
- js取得当前url,javascript获取当前页面url值,js获取域名
- js取得当前url,javascript获取当前页面url值,js获取域名
- JS获取当前URL地址
- js 获取当前URL地址
- 获取当前地址 JS获取当前URL
- 用js如何获取当前url
- js获取当前url地址的参数
- js获取当前页面URL地址
- js获取当前地址的url
- js获取当前域名、Url、相对路径和参数
- JS获取IP、IP所在城市、域名、完整URL、当前时间
- js获取当前域名、Url、相对路径和参数
- js获取当前域名
- Microsoft sqlserver sa无法登陆
- pThread=AfxBeginThread(ThreadFunc,&Info);
- Linux USB subsystem --- USB Debug File System Initialize
- hello,world!
- php特殊函数
- [JavaScript] 用js如何获取当前url地址中的域名
- 3ds max基本操作
- 如何用ResourceBundle来读取配置文件
- 正確避免浮點數計算偏差問題
- 如何除去HTML标签并保留文字
- 数据结构的C实现_顺序表
- 数据结构的C实现_单向链表
- 数据结构的C实现_双向循环链表
- 第五周实验报告(3)