您的位置:首页 > Web前端 > JavaScript

如下是各种获取页面和项目的路径和链接路径:

2016-01-07 15:38 507 查看
//获取当前网址,如: http://localhost:8088/test/test.jsp

var curPath=window.document.location.href;

//获取主机地址之后的目录,如: test/test.jsp

    var pathName=window.document.location.pathname;

alert("主机之后的地址:"+pathName);

    var pos=curPath.indexOf(pathName);

    //获取主机地址,如: http://localhost:8088

    alert("pos:"+pos);

var localhostPaht=curPath.substring(0,pos);

    alert("当前主机地址:"+localhostPaht);

    //获取带"/"的项目名,如:/test

    var projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1);

    alert("projectName:"+projectName);


javascript和jquery如何获取当前页面的url

2014-11-25 02:41 ·AB叔 1274 9 <-
点击左侧的数字“攒”一个吧

按照地址为: http://www.abshu.com:8082/index.php#tab2?foo=123 为例


Javascript:

代码								返回
--------------------------------------------------------------------------
window.location.host				www.abshu.com:8082
window.location.hostname			www.abshu.com
window.location.port				8082
window.location.protocol			http
window.location.pathname			index.php
window.location.href			 http://www.abshu.com:8082/index.php#tab2 window.location.hash				#tab2
window.location.search				?foo=123


Jquery:

代码								返回
--------------------------------------------------------------------------
$(location).attr('host');			www.test.com:8082
$(location).attr('hostname');		www.test.com
$(location).attr('port');			8082
$(location).attr('protocol');		http
$(location).attr('pathname');		index.php
$(location).attr('href');		 http://www.test.com:8082/index.php#tab2 $(location).attr('hash');			#tab2
$(location).attr('search');			?foo=123


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  javascript