简体   繁体   English

JavaScript中没有任何参数如何获得URL?

[英]How to get the URL without any parameters in JavaScript?

If I use:如果我使用:

alert(window.location.href);

I get everything including query strings.我得到了包括查询字符串在内的所有内容。 Is there a way to just get the main url part, for example:有没有办法获得主要的 url 部分,例如:

http://mysite.com/somedir/somefile/

instead of代替

http://mysite.com/somedir/somefile/?foo=bar&loo=goo

This is possible, but you'll have to build it manually from the location object :这是可能的,但您必须从object location手动构建它:

location.protocol + '//' + location.host + location.pathname

Every answer is rather convoluted.每个答案都相当复杂。 Here:这里:

var url = window.location.href.split('?')[0];

Even if a?即使一个? isn't present, it'll still return the first argument, which will be your full URL, minus query string.不存在,它仍然会返回第一个参数,这将是您的完整 URL,减去查询字符串。

It's also protocol-agnostic, meaning you could even use it for things like ftp, itunes.etc.它也与协议无关,这意味着您甚至可以将它用于 ftp、itunes.etc 等。

Use indexOf使用indexOf

var url = "http://mysite.com/somedir/somefile/?aa";

if (url.indexOf("?")>-1){
url = url.substr(0,url.indexOf("?"));
}

I'm LATE to the party, but I had to solve this recently, figured I'd share the wealth.我迟到了,但我最近不得不解决这个问题,我想我会分享财富。

const url = window.location.origin + window.location.pathname
//http://example.com/somedir/somefile/

window.location.origin will give you the base url, in our test case: http://example.com window.location.origin将为您提供基础 url,在我们的测试案例中: http://example.com

window.location.pathname will give you the route path (after the base url), in our test case /somedir/somefile window.location.pathname将为您提供路由路径(在基本 url 之后),在我们的测试用例/somedir/somefile

SOLUTION 2解决方案 2

You can simply do the following to get rid of the query parameters.您可以简单地执行以下操作来摆脱查询参数。

const url = window.location.href.split('?')[0]

You can concat origin and pathname , if theres present a port such as example.com:80 , that will be included as well.您可以连接originpathname ,如果存在诸如example.com:80之类的端口,也将包括在内。

location.origin + location.pathname
var url = "tp://mysite.com/somedir/somefile/?foo=bar&loo=goo"    

url.substring(0,url.indexOf("?"));

You can use a regular expression: window.location.href.match(/^[^\#\?]+/)[0]您可以使用正则表达式: window.location.href.match(/^[^\#\?]+/)[0]

Just one more alternative using URL使用 URL 的另一种选择

var theUrl = new URL(window.location.href);
theUrl.search = ""; //Remove any params
theUrl //as URL object
theUrl.href //as a string

If you look at the documentation you can take just the properties you're interested in from the window object ie如果您查看文档,您可以从window object 中获取您感兴趣的属性,即

protocol + '//' + hostname + pathname

Use the URL() constructor, then extract and concatenate the origin and pathname.使用URL()构造函数,然后提取并连接源和路径名。 This will automatically strip the search (aka query) parameters from the url, leaving the scheme, domain, port and pathname only.这将自动从 url 中去除搜索(也称为查询)参数,只留下方案、域、端口和路径名。

 const url = new URL('http://localhost:8080/index.html?search=foo&other=bar'); console.log(url.origin + url.pathname);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM