我有eq的域名.
1) http://www.abc.com/search
2) http://go.abc.com/work
Run Code Online (Sandbox Code Playgroud)
我只从上面的URL获得域名
输出就好
1) http://www.abc.com/
2) http://go.abc.com/
Run Code Online (Sandbox Code Playgroud)
我能怎么做?
Arn*_*anc 83
您可以使用<a>元素利用浏览器的URL解析器:
var hostname = $('<a>').prop('href', url).prop('hostname');
Run Code Online (Sandbox Code Playgroud)
或者没有jQuery:
var a = document.createElement('a');
a.href = url;
var hostname = a.hostname;
Run Code Online (Sandbox Code Playgroud)
(此技巧对于解析相对于当前页面的路径特别有用.)
使用以下功能:
function get_hostname(url) {
var m = url.match(/^http:\/\/[^/]+/);
return m ? m[0] : null;
}
Run Code Online (Sandbox Code Playgroud)
像这样使用它:
get_hostname("http://example.com/path");
Run Code Online (Sandbox Code Playgroud)
这将http://example.com/在示例输出中返回.
如果您只是尝试获取当前页面的主机名,请使用document.location.hostname.
Ada*_*oza 40
这对我有用.
http://tech-blog.maddyzone.com/javascript/get-current-url-javascript-jquery
$(location).attr('host'); www.test.com:8082
$(location).attr('hostname'); www.test.com
$(location).attr('port'); 8082
$(location).attr('protocol'); http
$(location).attr('pathname'); index.php
$(location).attr('href'); http://www.test.com:8082/index.php#tab2
$(location).attr('hash'); #tab2
$(location).attr('search'); ?foo=123
Run Code Online (Sandbox Code Playgroud)
你可以使用普通的js来做到这一点
location.host , 与...一样 document.location.hostname试试这样吧.
var hostname = window.location.origin
If the URL is "http://example.com/path" then you will get "http://example.com" as the result.
Run Code Online (Sandbox Code Playgroud)
这不适用于本地域
When you have URL like "https://localhost/MyProposal/MyDir/MyTestPage.aspx"
and your virtual directory path is "https://localhost/MyProposal/"
In such cases, you will get "https://localhost".
Run Code Online (Sandbox Code Playgroud)
小智 7
你不需要jQuery,因为简单的javascript就足够了:
alert(document.domain);
Run Code Online (Sandbox Code Playgroud)
看到它的实际效果:
console.log("Output;");
console.log(location.hostname);
console.log(document.domain);
alert(window.location.hostname)
console.log("document.URL : "+document.URL);
console.log("document.location.href : "+document.location.href);
console.log("document.location.origin : "+document.location.origin);
console.log("document.location.hostname : "+document.location.hostname);
console.log("document.location.host : "+document.location.host);
console.log("document.location.pathname : "+document.location.pathname);
Run Code Online (Sandbox Code Playgroud)
有关详细信息,请单击此处window.location
只需在域名前附加"http://"即可获得适当的结果.
您可以通过创建<a>-element,然后将字符串设置为该<a>-element 的href 来使用技巧,然后有一个Location对象,您可以从中获取主机名。
您可以将方法添加到String原型:
String.prototype.toLocation = function() {
var a = document.createElement('a');
a.href = this;
return a;
};
Run Code Online (Sandbox Code Playgroud)
并像这样使用它:
"http://www.abc.com/search".toLocation().hostname
或使其功能:
function toLocation(url) {
var a = document.createElement('a');
a.href = url;
return a;
};
Run Code Online (Sandbox Code Playgroud)
并像这样使用它:
toLocation("http://www.abc.com/search").hostname
这两个都将输出: "www.abc.com"
如果您还需要该协议,则可以执行以下操作:
var url = "http://www.abc.com/search".toLocation();
url.protocol + "//" + url.hostname
Run Code Online (Sandbox Code Playgroud)
将输出: "http://www.abc.com"
| 归档时间: |
|
| 查看次数: |
115349 次 |
| 最近记录: |