此代碼打開一個新窗口和監聽事件beforeunload做出一個XMLHttpRequest,並帶來新的內容......
<!DOCTYPE html>
<html>
<body>
<p>Click the button to open the new window.</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
var newWindow = window.open("popup.htm", "popup", "width=200, height=100");
console.log("LOG 1:"+newWindow.location.href);
newWindow.addEventListener("beforeunload",function(event){
loadNew();
}, true);
}
function loadNew(){
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
document.getElementsByTagName("html")[0].innerHTML = xhttp.responseText;
}
};
xhttp.open("GET", "popup.htm", true);
xhttp.send();
}
</script>
</body>
</html>
我就是這麼做的,因爲window.location.href="newLocation.html"
沒有爲我工作..
我會在當前窗口中使用模態窗口,而不是打開新窗口/選項卡。 – jeroen