[英]Javascript detect variable change after # in address bar
例:
http://localhost/#view=1
如果要获取该变量“ view”,则需要获取window.location.href
,然后拆分#
并执行其他操作。
这是我的问题:
<a href="#view=1">View 1</a>
<a href="#view=2">View 2</a>
无论如何,是否可以检测“视图1”单击或“视图2”单击的时间? 对于单击View 2的用户,将为view=2
一个功能。
或者我必须做这样的事情:
<a href="#view=1" onclick="setview(1);">View 1</a>
<a href="#view=2" onclick="setview(2);">View 2</a>
我想当人们复制链接http://localhost/#view=2
并发送给那里的朋友时,当他们的朋友访问URL时将运行setview(2)
函数,而无需单击链接。
hashChange
事件 $(function(){
// Bind the event.
$(window).hashchange( function(){
// Alerts every time the hash changes!
alert( location.hash );
})
// Trigger the event (useful on page load).
$(window).hashchange();
});
现在在$(window).hashchange();
功能,把你的代码:
$(window).hashchange(function(){
$(location.hash).show();
});
在您的情况下,它将是:
$(window).hashchange(function(){
setview(location.hash[strlen(location.hash)-1]); // Get the last character, if this doesn't work.
});
忘了添加插件: http : //benalman.com/projects/jquery-hashchange-plugin/ ,您需要使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.