This question already has an answer here:
-
Communication between tabs or windows
9 answers
I use the following to open new tab (in new process) with some page content,
var p = document.getElementById("myElement");
var a = document.createElement('a');
a.setAttribute('href',".../mypage.html");
a.setAttribute('rel',"noreferrer");
a.setAttribute('target',"_blank");
p.appendChild(a);
a.click();
http://news.softpedia.com/news/Force-Google-Chrome-to-Open-Links-in-New-Processes-128962.shtml
This is working and the new tab is open with myPage.html content.
Assume that this is myPage(just for sample...) how should I access it?
<!DOCTYPE html>
<html>
<body>
<h1> Heading</h1>
<p> paragraph.</p>
<button type="button">Click Me!</button>
</body>
</html>
Now Let's go to the tricky/advanced :) part...
when you use window.open
(which I cannot use )this is quite simple since you can use various techniques .
1. using window object
2. post message
https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage
3. cookies
4. localStorage
But here I open this new page without the reference which is got with window.open
My question is:
How can I access to this new tab dom if I want to change something
I faced a similar issue and build a small lib to make function calls via localStorage with paramter possible. You can find it here.
Service worker are currently not supported by all browsers.
Here is an example how to use it:
//Register a function:
RegisterLocalStorageFunction("test", function(param){
return param+param;
});
//Call a function:
let str = "testing";
CallLocalStorageFunction("test",str,function(para){
console.log(para);
});
In your context:
RegisterLocalStorageFunction("CallAlert", function(param){
alert(param);
return "Success";
});
var p = document.getElementById("myElement");
var a = document.createElement('a');
a.setAttribute('href',".../mypage.html");
a.setAttribute('rel',"noreferrer");
a.setAttribute('target',"_blank");
p.appendChild(a);
a.click();
In your other window:
<!DOCTYPE html>
<html>
<body>
<h1> Heading</h1>
<p> paragraph.</p>
<button type="button" onclick="btnClick()">Click Me!</button>
<script>
function btnclick(){
CallLocalStorageFunction("CallAlert","Hello from the other tab",function(para){
console.log("para");
});
}
</script>
</body>
</html>
Both sides must be on the same domain, otherwise they cant access the same localStorage. With my currrent code on github I use setInterval to cycle through the storage. There is a storage event, which is fired to all other tabs and windows, but not to the same tab. I'll rewrite the lib to use the much cleaner approach with the event, but for now this should do the trick.
Update
In the repository you can find communicator2, which is based on the 'storage' event.
Update
Here is a working example hosted. Keep in mind to allow popups.
Parent page JavaScript:
var lastMessage;
setInterval(function() {
var message = localStorage.getItem('message-to-parent');
if (message && message !== lastMessage) {
lastMessage = message;
// your code here
alert('There is a new message for you: ' + message);
}
}, 100);
Child page JavaScript:
localStorage.setItem('message-to-parent', 'hello, my parent!');
If you have a lot of animations and other huge JS code, I'd suggest to increase the timer interval, or better to solve the issue with window
.
You have to attach a DOMNodeInserted-listener before the click event is fired, then you can access this window-object.
// assuming that all inserted alements are links or a-tags
// this code must run before the code that adds the a-element
document.addEventListener("DOMNodeInserted", function (ev) {
var link = ev.target;
var oldRef = link.href;
link.onclick = function(event){
event.preventDefault();
var childWindow = window.open(oldRef);
childWindow.onload = function(){
console.log('at this point you can interact with this newly opened window-object: ',childWindow);
};
};
});
//...
// some other code
//...
var p = document.getElementById("myElement");
var a = document.createElement('a');
a.setAttribute('href',".../mypage.html");
a.setAttribute('rel',"noreferrer");
a.setAttribute('target',"_blank");
p.appendChild(a);
a.click();
Hope this helps.
You can use SharedWorker
to commuicate between different browsing contexts.
See Can we refer to javascript variables across webpages in a browser session?
You could alternatively use storage
event Can the mechanism that loads the worker in Shared Web Workers be modified?