![](/img/trans.png)
[英]How do I extract the base URL from a link-string with Javascript or jQuery?
[英]How to extract base URL from a string in JavaScript?
我试图找到一种相对简单可靠的方法来使用 JavaScript(或 jQuery)从字符串变量中提取基本 URL。
例如,给定如下内容:
http://www.sitename.com/article/2009/09/14/this-is-an-article/
我想得到:
http://www.sitename.com/
正则表达式是最好的选择吗? 如果是这样,我可以使用什么语句将从给定字符串中提取的基本 URL 分配给新变量?
我已经对此进行了一些搜索,但是我在 JavaScript 世界中找到的所有内容似乎都围绕使用location.host或类似方法从实际文档 URL 收集此信息。
编辑:有些人抱怨它没有考虑协议。 所以我决定升级代码,因为它被标记为答案。 对于那些喜欢单行代码的人……很抱歉,这就是我们使用代码最小化器的原因,代码应该是人类可读的,而且这种方式更好……在我看来。
var pathArray = "https://somedomain.com".split( '/' );
var protocol = pathArray[0];
var host = pathArray[2];
var url = protocol + '//' + host;
或者使用下面的Davids 解决方案。
基于 WebKit 的浏览器、Firefox 21 版和当前版本的 Internet Explorer(IE 10 和 11)都实现了location.origin
。
location.origin
包括协议、域和可选的 URL端口。
例如,URL http://www.sitename.com/article/2009/09/14/this-is-an-article/
location.origin
是http://www.sitename.com
。
要定位不支持location.origin
浏览器,请使用以下简洁的 polyfill:
if (typeof location.origin === 'undefined')
location.origin = location.protocol + '//' + location.host;
不需要使用jQuery,只需使用
location.hostname
没有理由进行拆分以从作为链接的字符串中获取路径、主机名等。 你只需要使用一个链接
//create a new element link with your link
var a = document.createElement("a");
a.href="http://www.sitename.com/article/2009/09/14/this-is-an-article/";
//hide it from view when it is added
a.style.display="none";
//add it
document.body.appendChild(a);
//read the links "features"
alert(a.protocol);
alert(a.hostname)
alert(a.pathname)
alert(a.port);
alert(a.hash);
//remove it
document.body.removeChild(a);
您可以使用 jQuery 附加元素并读取其属性轻松完成此操作。
更新:现在有new URL()
来简化它
const myUrl = new URL("https://www.example.com:3000/article/2009/09/14/this-is-an-article/#m123") const parts = ['protocol', 'hostname', 'pathname', 'port', 'hash']; parts.forEach(key => console.log(key, myUrl[key]))
var host = location.protocol + '//' + location.host + '/';
好吧, URL API 对象避免了手动拆分和构建 url。
let url = new URL('https://stackoverflow.com/questions/1420881');
alert(url.origin);
String.prototype.url = function() {
const a = $('<a />').attr('href', this)[0];
// or if you are not using jQuery 👇🏻
// const a = document.createElement('a'); a.setAttribute('href', this);
let origin = a.protocol + '//' + a.hostname;
if (a.port.length > 0) {
origin = `${origin}:${a.port}`;
}
const {host, hostname, pathname, port, protocol, search, hash} = a;
return {origin, host, hostname, pathname, port, protocol, search, hash};
}
然后 :
'http://mysite:5050/pke45#23'.url()
//OUTPUT : {host: "mysite:5050", hostname: "mysite", pathname: "/pke45", port: "5050", protocol: "http:",hash:"#23",origin:"http://mysite:5050"}
对于您的要求,您需要:
'http://mysite:5050/pke45#23'.url().origin
const parseUrl = (string, prop) => {
const a = document.createElement('a');
a.setAttribute('href', string);
const {host, hostname, pathname, port, protocol, search, hash} = a;
const origin = `${protocol}//${hostname}${port.length ? `:${port}`:''}`;
return prop ? eval(prop) : {origin, host, hostname, pathname, port, protocol, search, hash}
}
然后
parseUrl('http://mysite:5050/pke45#23')
// {origin: "http://mysite:5050", host: "mysite:5050", hostname: "mysite", pathname: "/pke45", port: "5050"…}
parseUrl('http://mysite:5050/pke45#23', 'origin')
// "http://mysite:5050"
凉爽的!
如果您使用 jQuery,这是一种在 javascript 中操作元素而不将它们添加到 DOM 的很酷的方法:
var myAnchor = $("<a />");
//set href
myAnchor.attr('href', 'http://example.com/path/to/myfile')
//your link's features
var hostname = myAnchor.attr('hostname'); // http://example.com
var pathname = myAnchor.attr('pathname'); // /path/to/my/file
//...etc
从 URL 的字符串表示中获取基本值的一种轻松但完整的方法是 Douglas Crockford 的正则表达式规则:
var yourUrl = "http://www.sitename.com/article/2009/09/14/this-is-an-article/";
var parse_url = /^(?:([A-Za-z]+):)?(\/{0,3})([0-9.\-A-Za-z]+)(?::(\d+))?(?:\/([^?#]*))?(?:\?([^#]*))?(?:#(.*))?$/;
var parts = parse_url.exec( yourUrl );
var result = parts[1]+':'+parts[2]+parts[3]+'/' ;
如果您正在寻找更强大的 URL 操作工具包,请尝试URI.js它支持 getter、setter、url 规范化等,所有这些都带有一个很好的可链接的 api。
如果您正在寻找 jQuery 插件,那么jquery.url.js应该可以帮助您
正如@epascarello 所建议的那样,一种更简单的方法是使用锚元素。 这样做的缺点是您必须创建一个 DOM 元素。 然而,这可以缓存在一个闭包中并重用于多个 url:
var parseUrl = (function () {
var a = document.createElement('a');
return function (url) {
a.href = url;
return {
host: a.host,
hostname: a.hostname,
pathname: a.pathname,
port: a.port,
protocol: a.protocol,
search: a.search,
hash: a.hash
};
}
})();
像这样使用它:
paserUrl('http://google.com');
如果您从 window.location.href(地址栏)中提取信息,则使用此代码获取http://www.sitename.com/
:
var loc = location;
var url = loc.protocol + "//" + loc.host + "/";
如果您有一个字符串str
,它是一个任意 URL(不是 window.location.href),则使用正则表达式:
var url = str.match(/^(([a-z]+:)?(\/\/)?[^\/]+\/).*$/)[1];
我和宇宙中的每个人一样,讨厌阅读正则表达式,所以我将用英语对其进行分解:
无需创建 DOM 元素或做任何疯狂的事情。
您可以使用以下代码获取当前 URL 的不同参数
alert("document.URL : "+document.URL);
alert("document.location.href : "+document.location.href);
alert("document.location.origin : "+document.location.origin);
alert("document.location.hostname : "+document.location.hostname);
alert("document.location.host : "+document.location.host);
alert("document.location.pathname : "+document.location.pathname);
我使用一个简单的正则表达式从 url 中提取主机:
function get_host(url){
return url.replace(/^((\w+:)?\/\/[^\/]+\/?).*$/,'$1');
}
并像这样使用它
var url = 'http://www.sitename.com/article/2009/09/14/this-is-an-article/'
var host = get_host(url);
请注意,如果url
不以/
结尾,则host
不会以/
结尾。
以下是一些测试:
describe('get_host', function(){
it('should return the host', function(){
var url = 'http://www.sitename.com/article/2009/09/14/this-is-an-article/';
assert.equal(get_host(url),'http://www.sitename.com/');
});
it('should not have a / if the url has no /', function(){
var url = 'http://www.sitename.com';
assert.equal(get_host(url),'http://www.sitename.com');
});
it('should deal with https', function(){
var url = 'https://www.sitename.com/article/2009/09/14/this-is-an-article/';
assert.equal(get_host(url),'https://www.sitename.com/');
});
it('should deal with no protocol urls', function(){
var url = '//www.sitename.com/article/2009/09/14/this-is-an-article/';
assert.equal(get_host(url),'//www.sitename.com/');
});
it('should deal with ports', function(){
var url = 'http://www.sitename.com:8080/article/2009/09/14/this-is-an-article/';
assert.equal(get_host(url),'http://www.sitename.com:8080/');
});
it('should deal with localhost', function(){
var url = 'http://localhost/article/2009/09/14/this-is-an-article/';
assert.equal(get_host(url),'http://localhost/');
});
it('should deal with numeric ip', function(){
var url = 'http://192.168.18.1/article/2009/09/14/this-is-an-article/';
assert.equal(get_host(url),'http://192.168.18.1/');
});
});
一个好方法是使用 JavaScript 原生 api URL
对象。 这提供了许多有用的 url 部分。
例如:
const url = 'https://stackoverflow.com/questions/1420881/how-to-extract-base-url-from-a-string-in-javascript'
const urlObject = new URL(url);
console.log(urlObject);
// RESULT:
//________________________________
hash: "",
host: "stackoverflow.com",
hostname: "stackoverflow.com",
href: "https://stackoverflow.com/questions/1420881/how-to-extract-base-url-from-a-string-in-javascript",
origin: "https://stackoverflow.com",
password: "",
pathname: "/questions/1420881/how-to-extract-base-url-from-a-string-in-javaript",
port: "",
protocol: "https:",
search: "",
searchParams: [object URLSearchParams]
... + some other methods
正如你在这里看到的,你可以访问任何你需要的东西。
例如: console.log(urlObject.host); // "stackoverflow.com"
console.log(urlObject.host); // "stackoverflow.com"
网址的文档
function getBaseURL() {
var url = location.href; // entire url including querystring - also: window.location.href;
var baseURL = url.substring(0, url.indexOf('/', 14));
if (baseURL.indexOf('http://localhost') != -1) {
// Base Url for localhost
var url = location.href; // window.location.href;
var pathname = location.pathname; // window.location.pathname;
var index1 = url.indexOf(pathname);
var index2 = url.indexOf("/", index1 + 1);
var baseLocalUrl = url.substr(0, index2);
return baseLocalUrl + "/";
}
else {
// Root Url for domain name
return baseURL + "/";
}
}
然后你可以像这样使用它......
var str = 'http://en.wikipedia.org/wiki/Knopf?q=1&t=2';
var url = str.toUrl();
url 的值将是...
{
"original":"http://en.wikipedia.org/wiki/Knopf?q=1&t=2",<br/>"protocol":"http:",
"domain":"wikipedia.org",<br/>"host":"en.wikipedia.org",<br/>"relativePath":"wiki"
}
“var url”还包含两个方法。
var paramQ = url.getParameter('q');
在这种情况下,paramQ 的值为 1。
var allParameters = url.getParameters();
allParameters 的值将仅为参数名称。
["q","t"]
在 IE、chrome 和 firefox 上测试。
不必考虑 window.location.protocol 和 window.location.origin,也不必考虑指定的端口号等,只需抓取第 3 个“/”之前的所有内容:
// get nth occurrence of a character c in the calling string
String.prototype.nthIndex = function (n, c) {
var index = -1;
while (n-- > 0) {
index++;
if (this.substring(index) == "") return -1; // don't run off the end
index += this.substring(index).indexOf(c);
}
return index;
}
// get the base URL of the current page by taking everything up to the third "/" in the URL
function getBaseURL() {
return document.URL.substring(0, document.URL.nthIndex(3,"/") + 1);
}
这有效:
location.href.split(location.pathname)[0];
您可以使用正则表达式来做到这一点:
/(http:\/\/)?(www)[^\/]+\//i
合身吗 ?
要获取任何 url 的来源,包括网站内的路径 ( /my/path
) 或无模式 ( //example.com/my/path
) 或完整 ( http://example.com/my/path
) 我把一起快速功能。
在下面的代码段中,所有三个调用都应记录https://stacksnippets.net
。
function getOrigin(url) { if(/^\\/\\//.test(url)) { // no scheme, use current scheme, extract domain url = window.location.protocol + url; } else if(/^\\//.test(url)) { // just path, use whole origin url = window.location.origin + url; } return url.match(/^([^/]+\\/\\/[^/]+)/)[0]; } console.log(getOrigin('https://stacksnippets.net/my/path')); console.log(getOrigin('//stacksnippets.net/my/path')); console.log(getOrigin('/my/path'));
这对我有用:
var getBaseUrl = function (url) { if (url) { var parts = url.split('://'); if (parts.length > 1) { return parts[0] + '://' + parts[1].split('/')[0] + '/'; } else { return parts[0].split('/')[0] + '/'; } } };
var tilllastbackslashregex = new RegExp(/^.*\//);
baseUrl = tilllastbackslashregex.exec(window.location.href);
window.location.href 从浏览器地址栏给出当前 url 地址
它可以是任何类似https://stackoverflow.com/abc/xyz或https://www.google.com/search?q=abc untillastbackslashregex.exec() 运行正则表达式并重新调整匹配的字符串直到最后一个反斜杠即https ://stackoverflow.com/abc/或https://www.google.com/分别
执行:
const getOriginByUrl = url => url.split('/').slice(0, 3).join('/');
测试:
getOriginByUrl('http://www.sitename.com:3030/article/2009/09/14/this-is-an-article?lala=kuku');
结果:
'http://www.sitename.com:3030'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.