在javascript中创建一个新的位置对象

时间:2022-09-25 08:49:30

Is it possible to create a new Location object in javascript? I have a url as a string and I would like to leverage what javascript already provides to gain access to the different parts of it.

是否可以在javascript中创建一个新的位置对象?我有一个作为字符串的url,我想利用javascript已经提供的资源来访问它的不同部分。

Here's an example of what I'm talking about (I know this doesn't work):

这里有一个我正在谈论的例子(我知道这行不通):

var url = new window.location("http://www.example.com/some/path?name=value#anchor");
var protocol = url.protocol;
var hash = url.hash;
// etc etc

Is anything like this possible or would I essentially have to create this object myself?

像这样的东西可能吗?或者我必须自己创建这个对象吗?

4 个解决方案

#1


114  

Well, you could use an anchor element to extract the url parts, for example:

那么,您可以使用锚元素来提取url部分,例如:

var url = document.createElement('a');
url.href = "http://www.example.com/some/path?name=value#anchor";
var protocol = url.protocol;
var hash = url.hash;

alert('protocol: ' + protocol);
alert('hash: ' + hash);
​

It works on all modern browsers and even on IE 5.5+.

它适用于所有现代浏览器,甚至IE 5.5+。

Check an example here.

检查一个例子。

#2


19  

How about use the standard URL object?

如何使用标准URL对象?

var url = new URL("http://www.example.com/some/path?name=value#anchor");
var protocol = url.protocol;
var hash = url.hash;

Warning: This interface is a bit new, so, if you're not using a transpiler, please, check the compatibility table and do your tests at target browsers.

警告:这个界面有点新,所以如果您不使用传输器,请检查兼容性表并在目标浏览器上进行测试。

#3


9  

You can leverage the power of an anchor element

您可以利用锚元素的力量

var aLink = document.createElement("a");
aLink.href="http://www.example.com/foo/bar.html?q=123#asdf";
alert(aLink.pathname);

#4


0  

You can parse it in a regex to get the parts as matches... I don't have the full code right now, but this can be used to get the querydata:

您可以在regex中解析它,以获得匹配的部分……我现在没有完整的代码,但是这可以用来获取querydata:

var myUrl = window.location.href;
var matches = myUrl.match(/([^\?]+)\?(.+)/);
var queryData = matches[2];

matches[0] is the full string, matches(1) is the first part of the URL (up to the ?)... you could build up a regular expression to parse each part of a string url if you want...

matches[0]是完整的字符串,matches(1)是URL的第一部分(直到?)…您可以构建一个正则表达式来解析字符串url的每个部分,如果您想……

You can also use one of the many libraries already out there for this.

您还可以使用已有的许多库中的一个。

#1


114  

Well, you could use an anchor element to extract the url parts, for example:

那么,您可以使用锚元素来提取url部分,例如:

var url = document.createElement('a');
url.href = "http://www.example.com/some/path?name=value#anchor";
var protocol = url.protocol;
var hash = url.hash;

alert('protocol: ' + protocol);
alert('hash: ' + hash);
​

It works on all modern browsers and even on IE 5.5+.

它适用于所有现代浏览器,甚至IE 5.5+。

Check an example here.

检查一个例子。

#2


19  

How about use the standard URL object?

如何使用标准URL对象?

var url = new URL("http://www.example.com/some/path?name=value#anchor");
var protocol = url.protocol;
var hash = url.hash;

Warning: This interface is a bit new, so, if you're not using a transpiler, please, check the compatibility table and do your tests at target browsers.

警告:这个界面有点新,所以如果您不使用传输器,请检查兼容性表并在目标浏览器上进行测试。

#3


9  

You can leverage the power of an anchor element

您可以利用锚元素的力量

var aLink = document.createElement("a");
aLink.href="http://www.example.com/foo/bar.html?q=123#asdf";
alert(aLink.pathname);

#4


0  

You can parse it in a regex to get the parts as matches... I don't have the full code right now, but this can be used to get the querydata:

您可以在regex中解析它,以获得匹配的部分……我现在没有完整的代码,但是这可以用来获取querydata:

var myUrl = window.location.href;
var matches = myUrl.match(/([^\?]+)\?(.+)/);
var queryData = matches[2];

matches[0] is the full string, matches(1) is the first part of the URL (up to the ?)... you could build up a regular expression to parse each part of a string url if you want...

matches[0]是完整的字符串,matches(1)是URL的第一部分(直到?)…您可以构建一个正则表达式来解析字符串url的每个部分,如果您想……

You can also use one of the many libraries already out there for this.

您还可以使用已有的许多库中的一个。