javascript - 在 javascript 或 angularjs 中解析 url

标签 javascript angularjs parsing url

我试图在 javascript 中解析 url,我发现了以下方法:

var getLocation = function(href) {
    var l = document.createElement("a");
    l.href = href;
    return l;
};
var l = getLocation("http://example.com:3000/path");
var host = l.host; // example.com
var port = l.port; // 3000

但如果这些位置:

http://TLVS0015:3000/cti/YTest // the parse found the port, but its not found the host

http://ctmwe:80/ReportServer/ReportService2010.asmx // the parse found the host, but don't found the port

还有其他方法可以解析吗?

最佳答案

来源:- 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.hostname; // => "example.com"
parser.port;     // => "3000"
parser.pathname; // => "/pathname/"
parser.search;   // => "?search=test"
parser.hash;     // => "#hash"
parser.host;     // => "example.com:3000"

关于javascript - 在 javascript 或 angularjs 中解析 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27940283/

相关文章:

javascript - Crossrider "appAPI.message.toAllTabs"方法的消息发送顺序

javascript - 当 url 目录更改时,Ajax 不向 php 发送请求

jquery - Angular JS 表单重新加载页面

使用 FParsec 解析日期和时间

javascript - 如何使图像拉伸(stretch)以填充 PhotoSwipe 中的可用区域

javascript - AngularJs 在重定向到新位置时更改后退按钮位置

javascript - AngularJS 函数 - $Scope

objective-c - 解析 Objective C 错误中的 JSON 值?

sql - 使用正则表达式解析 select 语句(用于自定义 SQL 解析器)

javascript - 将 JS 事件从一个元素复制到另一个元素