可以使用window.postMessage()
不同的瀏覽之間進行通信上下文
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<button>click</button>
<a href="page2.html" target="_blank">open page2</a>
<script>
var src;
document.querySelector("button")
.onclick = function() {
src.postMessage("clicked", src.location.href);
}
window.onmessage = function(e) {
console.log(e.data);
src = e.source;
}
</script>
</body>
</html>
頁2
<!DOCTYPE html>
<html>
<head>
</head>
<body>
page2
<script>
window.onload = function() {
if (this.opener.location.href === document.referrer) {
this.opener.postMessage("message from page2", document.referrer)
}
}
window.onmessage = function(e) {
console.log(e.data);
var audio = new Audio("/path/to/audio");
audio.oncanplay = function() {
this.play()
}
}
</script>
</body>
</html>
plnkr http://plnkr.co/edit/EsqQ8dPEQZoTHqhvE0Uy?p=preview
可以使用'的postMessage()'到瀏覽上下文之間進行通信。 – guest271314