2012-03-24 85 views
0

我试图做一个刷新页面(并重置测验)并跳转到具有按钮的锚点的函数。Javascript名称跳转锚点

这适用于Chrome和Firefox,但在IE中它只是刷新到第一个命名锚点。

我的代码:

function resetQuiz(showConfirm) 
{ 
    if(showConfirm) 
    if(!confirm("Are you sure you want to reset your answers and start from the beginning?")) 
     return false; 
window.location.reload(); //this works for chrome and firefox 
window.location.hash="mercquiz"; //tried this for IE. doesnt work 

} 
+1

IE版本....? – Hamish 2012-03-24 00:06:22

+0

尝试为“if”块放置花括号。我知道在ANSI C中可能是一个模糊的情况 – puk 2012-03-24 00:18:44

+0

IE版本是8.0 – Waiwhetu 2012-03-24 00:33:52

回答

2

你可能会重新加载页面的哈希有机会更新之前。

试试这个:

var loc = window.location.toString(); 
window.location = loc.substring(0, loc.indexOf("#")) + "#mercquiz"; 
window.location.reload(); 
+1

这似乎不适用于任何浏览器。它只是将#mercquiz附加到网址(test.html#mercquiz#mercquiz)为了澄清,我的测验在test.html#mercquiz上,重置按钮位于该主分区。 – Waiwhetu 2012-03-24 00:31:34

+0

奇怪...通常更改'window.location'属性将强制重新加载。我会研究它。 – 2012-03-24 00:40:49

+0

@Waiwhetu好的,现在就试试吧。它更新散列,然后重新加载页面。 – 2012-03-24 00:52:07