Javascript 为什么';这不管用吗?

Javascript 为什么';这不管用吗?,javascript,html,Javascript,Html,我正在尝试使用pushstate,但它不起作用: <html> <head> <script>function myFunction() { alert("lel"); var stateObj = { foo: "bar" }; window.history.pushState(stateObj, "page 2", "bar.html"); } </script> </head> <body onload="myFunct

我正在尝试使用pushstate,但它不起作用:

<html>
<head>
<script>function myFunction() {
alert("lel"); 
var stateObj = { foo: "bar" };
window.history.pushState(stateObj, "page 2", "bar.html");
} 
</script>
</head>
<body onload="myFunction()"></body>
</html>

函数myFunction(){
警报(“lel”);
var stateObj={foo:“bar”};
window.history.pushState(stateObj,“第2页”,“bar.html”);
} 
警报出现,因此我知道该功能正在执行,但浏览器的地址栏没有发生任何变化。为什么?


编辑:我使用Firefox、Chrome和IE进行了测试。该脚本只在IE上运行,在Firefox或Chrome上不运行

您必须从HTTP服务器请求页面,而不是直接从磁盘请求,否则它将无法工作。谢谢你,乔纳森

它对我有用。您在哪个浏览器中测试?
pushstate
不受广泛支持。IE10和FF和chrome一样支持它,但在IE10之下没有任何东西。你使用的是什么浏览器?我使用的是firefox 23.0.1。你是如何请求页面的?从HTTP服务器?直接从磁盘?如果您使用的是
文件://
,推送状态可能就是不允许的--
安全错误:操作不安全。