16
How to identify if any link originated from a iframe
that I do not have access was opened in a new guide?
I tried with document.getElementById("i").addEventListener("click", a);
, but from what I understand it is not possible to use onclick
in a iframe
unless it has direct access to the source code of the iframe
I tried with a div
override but undo the first click on iframe
it takes more than one click to open the link
So I looked for how to identify if a link was opened in a new tab and found nothing like it.
The user of my page has to complete a task on iframe
when finishing the task within the iframe
a page will open in a new tab, how to identify if the tab has been opened?
Example snippet:
document.getElementById("i").onclick = function a(){
console.log("ok")
}
Preencha com seu nome e conclua<br>
<iframe id="i" src="https://editor.sollic.com/iframe.html"></iframe>
The closest I came was through a div
superimposing the iframe
.
Sample page: https://editor.sollic.com/stackoverflow
Explaining the code:
When passing the mouse over the div
it will disappear by 1 second whenever the mouse is on it allowing you to click anywhere until the page goes out of focus, with this if the div
is hidden and the out-of-focus page will activate the "trigger" who will send "ok" on console or in case of example link will change the background color to red.
But there are times it works correctly and in others it doesn’t work or it works wrong, like clicking anywhere and receiving the console message or changing the background color.
Snippet does not work properly because stackoverflow blocks.
var foco = true;
function con() { //função que será ativada ao passar o mouse
window.onblur = function(){foco = false} //verifica se a pagina está fora de foco
window.onfocus = function(){foco = true} //verifica se a pagina está em foco
if(foco!=false) {document.getElementById("a").style.display='block'} //se a pagina estiver em foco reativa a div
else {console.log("ok")} //se a pagina estiver em foco envia "ok" no console
}
function la() {document.getElementById("a").style.display='none'} //desativa a div
#a{
position:absolute;
z-index:1;
background:#000;
width:304px;
height:155px
}
<div id="a" onmouseover="setTimeout(con, 1000);la();"></div>
<iframe id="i" src="https://editor.sollic.com/iframe.html"></iframe>
Just a curiosity: what action you wanted to take when you detected that the new tab was opened?
– Sam
At the moment the page will be redirected to the previous page
– Mark Vaaz
He’s young, not to be disheartening, but I don’t think that’s possible. An iframe is like a separate document inside another. The parent document does not hear events in the child document, unless it was of the same domain. When iframe opens a new tab, this event goes over the parent document, and does not create any kind of relationship between the parent document and the iframe document. The only event I can see that creates a certain connection between the two documents is the
window.onblur
, since, when a new one is opened, theblur
is fired in the document of the tab that was active.– Sam
But the
blur
tb is fired when you change tab, there is no way to know if the event was triggered because of iframe.– Sam
I imagined that Blur would look like this, but if I have the address of the page that will open, it is possible to access the browser history if the page was opened?
– Mark Vaaz
There is no way to access the history via JS, even for security and privacy reasons. Ever thought a page to know which sites you have been visiting?
– Sam
@Sam I did otherwise as described in the question, but it does not always work and I could not find what causes this problem, would be the browser cache or something like?
– Mark Vaaz
For me you should give this up. There is no way to function, for the reasons I described above. You will never know when a new tab has been opened by iframe.
– Sam
All the answers are good, but they are nothing more than what I commented above =]
– Sam
@Sam really are, but I offered the reward for viable ways to do that, like my code in question.
– Mark Vaaz