fre*_*ara 345 javascript url
我想拿个字符串
var a = "http://example.com/aa/bb/"
Run Code Online (Sandbox Code Playgroud)
并将其处理成一个对象
a.hostname == "example.com"
Run Code Online (Sandbox Code Playgroud)
和
a.pathname == "/aa/bb"
Run Code Online (Sandbox Code Playgroud)
fre*_*ara 356
var getLocation = function(href) {
var l = document.createElement("a");
l.href = href;
return l;
};
var l = getLocation("http://example.com/path");
console.debug(l.hostname)
>> "example.com"
console.debug(l.pathname)
>> "/path"
Run Code Online (Sandbox Code Playgroud)
rvi*_*hne 334
现代方式:
new URL("http://example.com/aa/bb/")
Run Code Online (Sandbox Code Playgroud)
返回与属性的对象hostname和pathname,连同其他几个人.
第一个参数是相对或绝对URL; 如果它是相对的,那么你需要指定第二个参数(基本URL).例如,对于相对于当前页面的URL:
new URL("/aa/bb/", location)
Run Code Online (Sandbox Code Playgroud)
除了浏览器之外,从v7开始,此API也可在Node.js中使用require('url').URL.
Jos*_*ter 287
在这里找到:https://gist.github.com/jlong/2428561
var parser = document.createElement('a');
parser.href = "http://example.com:3000/pathname/?search=test#hash";
parser.protocol; // => "http:"
parser.host; // => "example.com:3000"
parser.hostname; // => "example.com"
parser.port; // => "3000"
parser.pathname; // => "/pathname/"
parser.hash; // => "#hash"
parser.search; // => "?search=test"
parser.origin; // => "http://example.com:3000"
Run Code Online (Sandbox Code Playgroud)
Rem*_*ems 102
这是一个使用模仿a标记行为的正则表达式的简单函数.
优点
缺点
-
function getLocation(href) {
var match = href.match(/^(https?\:)\/\/(([^:\/?#]*)(?:\:([0-9]+))?)([\/]{0,1}[^?#]*)(\?[^#]*|)(#.*|)$/);
return match && {
href: href,
protocol: match[1],
host: match[2],
hostname: match[3],
port: match[4],
pathname: match[5],
search: match[6],
hash: match[7]
}
}
Run Code Online (Sandbox Code Playgroud)
-
getLocation("http://example.com/");
/*
{
"protocol": "http:",
"host": "example.com",
"hostname": "example.com",
"port": undefined,
"pathname": "/"
"search": "",
"hash": "",
}
*/
getLocation("http://example.com:3000/pathname/?search=test#hash");
/*
{
"protocol": "http:",
"host": "example.com:3000",
"hostname": "example.com",
"port": "3000",
"pathname": "/pathname/",
"search": "?search=test",
"hash": "#hash"
}
*/
Run Code Online (Sandbox Code Playgroud)
编辑:
这是正则表达式的细分
var reURLInformation = new RegExp([
'^(https?:)//', // protocol
'(([^:/?#]*)(?::([0-9]+))?)', // host (hostname and port)
'(/{0,1}[^?#]*)', // pathname
'(\\?[^#]*|)', // search
'(#.*|)$' // hash
].join(''));
var match = href.match(reURLInformation);
Run Code Online (Sandbox Code Playgroud)
Pet*_*ham 63
var loc = window.location; // => "http://example.com:3000/pathname/?search=test#hash"
Run Code Online (Sandbox Code Playgroud)
返回currentUrl.
如果要将自己的字符串作为url传递(在IE11中不起作用):
var loc = new URL("http://example.com:3000/pathname/?search=test#hash")
Run Code Online (Sandbox Code Playgroud)
然后你可以解析它:
loc.protocol; // => "http:"
loc.host; // => "example.com:3000"
loc.hostname; // => "example.com"
loc.port; // => "3000"
loc.pathname; // => "/pathname/"
loc.hash; // => "#hash"
loc.search; // => "?search=test"
Run Code Online (Sandbox Code Playgroud)
Cla*_*aus 59
freddiefujiwara的答案非常好,但我还需要支持Internet Explorer中的相对URL.我提出了以下解决方案:
function getLocation(href) {
var location = document.createElement("a");
location.href = href;
// IE doesn't populate all link properties when setting .href with a relative URL,
// however .href will return an absolute URL which then can be used on itself
// to populate these additional fields.
if (location.host == "") {
location.href = location.href;
}
return location;
};
Run Code Online (Sandbox Code Playgroud)
现在用它来获取所需的属性:
var a = getLocation('http://example.com/aa/bb/');
document.write(a.hostname);
document.write(a.pathname);
Run Code Online (Sandbox Code Playgroud)
JSFiddle示例:http://jsfiddle.net/6AEAB/
Rex*_*x M 17
js-uri(可在Google Code上获得)获取字符串URL并从中解析URI对象:
var some_uri = new URI("http://www.example.com/foo/bar");
alert(some_uri.authority); // www.example.com
alert(some_uri); // http://www.example.com/foo/bar
var blah = new URI("blah");
var blah_full = blah.resolve(some_uri);
alert(blah_full); // http://www.example.com/foo/blah
Run Code Online (Sandbox Code Playgroud)
sve*_*tka 12
简单的正则表达怎么样?
url = "http://www.example.com/path/to/somwhere";
urlParts = /^(?:\w+\:\/\/)?([^\/]+)(.*)$/.exec(url);
hostname = urlParts[1]; // www.example.com
path = urlParts[2]; // /path/to/somwhere
Run Code Online (Sandbox Code Playgroud)
这是我从https://gist.github.com/1847816复制的版本,但重写后更容易阅读和调试.将锚数据复制到另一个名为"result"的变量的目的是因为锚数据非常长,因此将有限数量的值复制到结果将有助于简化结果.
/**
* See: https://gist.github.com/1847816
* Parse a URI, returning an object similar to Location
* Usage: var uri = parseUri("hello?search#hash")
*/
function parseUri(url) {
var result = {};
var anchor = document.createElement('a');
anchor.href = url;
var keys = 'protocol hostname host pathname port search hash href'.split(' ');
for (var keyIndex in keys) {
var currentKey = keys[keyIndex];
result[currentKey] = anchor[currentKey];
}
result.toString = function() { return anchor.href; };
result.requestUri = result.pathname + result.search;
return result;
}
Run Code Online (Sandbox Code Playgroud)
今天我遇到了这个问题,我发现:URL - MDN Web API
var url = new URL("http://test.example.com/dir/subdir/file.html#hash");
Run Code Online (Sandbox Code Playgroud)
这个回报:
{ hash:"#hash", host:"test.example.com", hostname:"test.example.com", href:"http://test.example.com/dir/subdir/file.html#hash", origin:"http://test.example.com", password:"", pathname:"/dir/subdir/file.html", port:"", protocol:"http:", search: "", username: "" }
Run Code Online (Sandbox Code Playgroud)
希望我的第一个贡献可以帮到你!
function parseUrl(url) {
var m = url.match(/^(([^:\/?#]+:)?(?:\/\/((?:([^\/?#:]*):([^\/?#:]*)@)?([^\/?#:]*)(?::([^\/?#:]*))?)))?([^?#]*)(\?[^#]*)?(#.*)?$/),
r = {
hash: m[10] || "", // #asd
host: m[3] || "", // localhost:257
hostname: m[6] || "", // localhost
href: m[0] || "", // http://username:password@localhost:257/deploy/?asd=asd#asd
origin: m[1] || "", // http://username:password@localhost:257
pathname: m[8] || (m[1] ? "/" : ""), // /deploy/
port: m[7] || "", // 257
protocol: m[2] || "", // http:
search: m[9] || "", // ?asd=asd
username: m[4] || "", // username
password: m[5] || "" // password
};
if (r.protocol.length == 2) {
r.protocol = "file:///" + r.protocol.toUpperCase();
r.origin = r.protocol + "//" + r.host;
}
r.href = r.origin + r.pathname + r.search + r.hash;
return r;
};
parseUrl("http://username:password@localhost:257/deploy/?asd=asd#asd");
Run Code Online (Sandbox Code Playgroud)
它适用于绝对和相对网址
跨浏览器URL解析,解决IE 6,7,8和9 的相对路径问题:
function ParsedUrl(url) {
var parser = document.createElement("a");
parser.href = url;
// IE 8 and 9 dont load the attributes "protocol" and "host" in case the source URL
// is just a pathname, that is, "/example" and not "http://domain.com/example".
parser.href = parser.href;
// IE 7 and 6 wont load "protocol" and "host" even with the above workaround,
// so we take the protocol/host from window.location and place them manually
if (parser.host === "") {
var newProtocolAndHost = window.location.protocol + "//" + window.location.host;
if (url.charAt(1) === "/") {
parser.href = newProtocolAndHost + url;
} else {
// the regex gets everything up to the last "/"
// /path/takesEverythingUpToAndIncludingTheLastForwardSlash/thisIsIgnored
// "/" is inserted before because IE takes it of from pathname
var currentFolder = ("/"+parser.pathname).match(/.*\//)[0];
parser.href = newProtocolAndHost + currentFolder + url;
}
}
// copies all the properties to this object
var properties = ['host', 'hostname', 'hash', 'href', 'port', 'protocol', 'search'];
for (var i = 0, n = properties.length; i < n; i++) {
this[properties[i]] = parser[properties[i]];
}
// pathname is special because IE takes the "/" of the starting of pathname
this.pathname = (parser.pathname.charAt(0) !== "/" ? "/" : "") + parser.pathname;
}
Run Code Online (Sandbox Code Playgroud)
用法(这里演示JSFiddle):
var myUrl = new ParsedUrl("http://www.example.com:8080/path?query=123#fragment");
Run Code Online (Sandbox Code Playgroud)
结果:
{
hash: "#fragment"
host: "www.example.com:8080"
hostname: "www.example.com"
href: "http://www.example.com:8080/path?query=123#fragment"
pathname: "/path"
port: "8080"
protocol: "http:"
search: "?query=123"
}
Run Code Online (Sandbox Code Playgroud)
对于那些寻找适用于IE,Firefox和Chrome的现代解决方案的人:
这些使用超链接元素的解决方案在chrome中都不会起作用.如果将无效(或空白)URL传递给chrome,它将始终返回调用脚本的主机.因此,在IE中,您将获得空白,而在Chrome中,您将获得localhost(或其他).
如果您试图查看推荐人,这是欺骗性的.您需要确保您获得的主机位于原始网址中以处理此问题:
function getHostNameFromUrl(url) {
// <summary>Parses the domain/host from a given url.</summary>
var a = document.createElement("a");
a.href = url;
// Handle chrome which will default to domain where script is called from if invalid
return url.indexOf(a.hostname) != -1 ? a.hostname : '';
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
350061 次 |
| 最近记录: |