sai*_*int 250 javascript url
我有一个像这样的网址http://localhost/dms/mduserSecurity/UIL/index.php?menu=true&submenu=true&pcode=1235.
我想没有查询字符串的网址:http://localhost/dms/mduserSecurity/UIL/index.php.
在JavaScript中有没有这方法?目前我正在使用document.location.href,但它返回完整的URL.
Fel*_*ing 368
阅读Window.location和Location界面:
var url = [location.protocol, '//', location.host, location.pathname].join('');
Run Code Online (Sandbox Code Playgroud)
tra*_*lix 314
试试这个: window.location.href.split('?')[0]
Que*_*tin 30
location.toString().replace(location.search, "")
Run Code Online (Sandbox Code Playgroud)
Jas*_*son 10
var url = window.location.origin + window.location.pathname;
Run Code Online (Sandbox Code Playgroud)
s4y*_*s4y 10
这是使用URL() 接口的方法:
new URL(location.pathname, location.href).href
Run Code Online (Sandbox Code Playgroud)
只需使用 split 来切割字符串(简单的方法):
var myString = "http://localhost/dms/mduserSecurity/UIL/index.php?menu=true&submenu=true&pcode=1235"
var mySplitResult = myString.split("?");
alert(mySplitResult[0]);
Run Code Online (Sandbox Code Playgroud)
尝试:
document.location.protocol + '//' +
document.location.host +
document.location.pathname;
Run Code Online (Sandbox Code Playgroud)
(注意:.host不是.hostname这样端口被列入过,如果需要的话)
要获取除查询之外的 URL 的每个部分:
var url = (location.origin).concat(location.pathname).concat(location.hash);
Run Code Online (Sandbox Code Playgroud)
请注意,这也包括散列,如果有的话(我知道您的示例 URL 中没有散列,但为了完整起见,我包含了该方面)。要消除散列,只需 exclude .concat(location.hash)。
使用concat将 Javascript 字符串连接在一起(而不是+)是更好的做法:在某些情况下,它可以避免诸如类型混淆之类的问题。
| 归档时间: |
|
| 查看次数: |
131259 次 |
| 最近记录: |