简体   繁体   中英

Why doesn't this javascript work?

I'm trying to use the pushstate but it doesn't work:

<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>

The alert comes up so I know the function is being executed, but nothing happens to the browser's address bar. Why?

EDIT: I tested this using Firefox, Chrome and IE. The script only works on IE and not on firefox or chrome.

You have to request the page from an HTTP server not directly from disk otherwise it wouldn't work. Thanks Jonathan.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM