日期:2014-05-16  浏览次数:20312 次

Js 解析URL
例如 http://localhost:8888/search?wd=s#top
如何用js正则得到如下信息:

scheme: http
slash: //
host: localhost
port: 8888
path: /search
query: wd=s
hash:top
------解决方案--------------------
<script type="text/javascript">
var parser = document.createElement('a');
parser.href = "http://localhost:8888/search?wd=s#top";
parser.protocol; // => "http:"
parser.host;    // => "localhost:8888"
parser.hostname;  // => "localhost"
parser.port;      // => "8888"
parser.pathname;  // => "/search"
parser.hash;      // => "#top"
parser.search;    // => "?wd=s"
</script>