javascript代码适用于IE和Chrome,但不适用于Firefox

javascript code works in IE and Chrome but not in Firefox

本文关键字:适用于 不适用 Firefox 代码 IE javascript Chrome      更新时间:2023-09-26

以下代码段在IE和Chrome中有效,但在Firefox中不起作用,我不知道为什么。

对于这个片段,当在IE和Chrome中打开时,网页中会出现两个指向谷歌的链接,但在Firefox中什么都不会出现。它位于body标记中的脚本标记中。

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。一旦你离开了原来的页面,你就无法返回并留在那里,而不会被重定向到雅虎。这就是IE和Chrome中的情况(这只是我的学习目的),但我不明白为什么它在Firefox中不起作用。当你从雅虎回到原始页面时,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>

尝试将1传递到forward方法:

window.history.forward(1);

如果它不起作用,那么你可能运气不好了,因为这似乎是浏览器制造商之间的可用性灰色区域。允许JavaScript(用户以外的东西)在没有他/她的直接参与的情况下更改当前页面是一个可用性问题——类似于在窗口下不断弹出