ajax 中的哈希更改事件 (javascript)
我实际上正在尝试使用户能够为页面添加书签,为此,我正在使用 javascript 的哈希更改事件。这是我的代码:
<script type="text/javascript">
function hashchk()
{
hashvalue=window.location.hash;
newhash="";
for(var i=1;i<hashvalue.length;i++)
{
newhash=newhash+hashvalue[i];
}
if(hashvalue!="")
{
window.location.replace("viewme.php?ppid="+newhash);
}
}
hashchk();
</script>
这里的一切都正常,除了当用户想要返回上一页时,他必须按浏览器的后退按钮两次而不是一次。
如果他在 http://www.example.com/abc.php#hello 第一次按后退按钮时,网址单独更改为 http://www.example.com/abc.php 但页面未加载。
但下次按下它时,它就正常了。 我希望他们只按一次。提前致谢。
I am actually trying to enable user to bookmark pages and for this, i am using hash change event of javascript. Here is my code:
<script type="text/javascript">
function hashchk()
{
hashvalue=window.location.hash;
newhash="";
for(var i=1;i<hashvalue.length;i++)
{
newhash=newhash+hashvalue[i];
}
if(hashvalue!="")
{
window.location.replace("viewme.php?ppid="+newhash);
}
}
hashchk();
</script>
Everything here is working except for the fact that, when the user wants to go back to the previous page, he has to press the browser's back button 2 times rather than once.
if he is in http://www.example.com/abc.php#hello
on 1st pressing back button, url alone changes to http://www.example.com/abc.php but the page does not load.
But on pressing it the next time, it comes properly.
I want them to press it only once. Thanx in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我对你的问题不太清楚。如果用户第一次访问带有哈希值的页面(例如#html3),那么您似乎正在尝试触发此功能,以便您可以使用服务器中的适当数据刷新他们的屏幕。这个问题本身似乎与 hashchange 事件没有直接关系。
测试此 HTML 后,我相信它可以按您的预期工作。每当页面加载并带有 #hash 时,它就会在查询字符串上使用它进行重定向。这也适用于页面刷新。
I wasn't entirely clear on your question. It seems like you are trying to fire this if a user comes to a page for the first time with a hash on it (like #html3) so you can refresh their screen with the appropriate data from the server. The question itself doesn't seem directly related to the hashchange event.
After testing this HTML, I believe it works as you expect. Whenever the page loads and has a #hash on it it will redirect it with it on the query string. This will also work for page refreshes.