以下代码片段在 IE 和 Chrome 中有效,但在 Firefox 中无效,我不知道为什么。
对于此代码段,当在 IE 和 Chrome 中打开时,网页中会出现两个指向 Google 的链接,但在 Firefox 中什么也没有出现。它位于正文标记中的脚本标记中。
var varOne = document.createElement("a")
varOne.setAttribute("href", "http://www.google.com")
varOne.innerText = "Google"
document.body.appendChild(varOne) //attach the node to the body
var varOne = document.createElement("a")
varOne.setAttribute("href", "http://www.google.com")
varOne.textContent = "Google"
document.getElementById("bodyID").appendChild(varOne) //attach the node to the body
对于这个,您单击按钮转到 yahoo.com。当您按下浏览器的后退按钮时,您会返回到先前的页面,但 javascript 将再次运行,迫使浏览器返回到 yahoo。一旦您离开了原始页面,您将无法返回并停留在那里而不会被重定向到 yahoo。这就是在 IE 和 Chrome 中发生的事情(我只是为了学习目的),但我不明白为什么它在 Firefox 中不起作用。当您从 Yahoo 返回原始页面时,Firefox 不会像其他两种浏览器那样强制浏览器前进。
<body>
<form>
<input type="button" onclick="javascript: window.location='http://www.yahoo.com' "/>
</form>
<script type="text/javascript">
alert("Problem?")
window.history.forward()
</script>
</body>