window.location.hash常用方法

来源:互联网 发布:数据共享该怎样连接 编辑:程序博客网 时间:2024/06/05 17:42

window.location.href是在js中经常见到的获取url链接的方式

而我们在一些url中却看到过类似的地址

http://www.abc.com/a/index.html#m2

其中的#m2 就是location.hash

loation.hash常与锚点联系起来使用


例如:

<head>    <script>         function getAnchor(anchor_name) {             window.location.hash=anchor_name;         }     </script> </head>
<body>     <a href="javascript:getAnchor('test');">点我测试</a>     <div style="height:2000px">test</div>     <a name="test">destination</a></body>

当点击 测试 按钮时,实现页面位置的改变


location.hash通过与iframe的结合 还能巧妙的解决js代理完全跨域的问题


虽然有些麻烦,但是却可以解决js完全跨域的问题


详见:利用iframe和location.hash实现跨域

1 0
原创粉丝点击