例如URL地址为:http:// 域名:端口号/index.html?tag=参数#锚点
1.获取完整路径:
var $href=window.location.href;
结果:http:// 域名:端口号/index.html?tag=参数#锚点
2.获取URL协议:
var $protocol=window.location.protocol;
结果:http
3.获取主机部分:
var $host=window.location.host;
结果:域名(如:bbs.csdn.net)
4.URL的端口部分:
var $port=window.location.port;
结果:端口号 (如果为默认则获取为空)
5.获取部分路径(文件地址):
var $pathname=window.location.pathname;
结果:/index.html
6.获取参数部分:
var $search=window.location.search;
结果:?tag=参数
7.获取锚点:
var $hash=window.location.hash;
结果:#锚点