4
How to detect the URL where the iframe
is being displayed and performing a function if the page is being displayed at a specific URL.
Example: when accessing the page https://www.../exemplo.html
(not necessarily of the same domain) one iframe
with the src='//www.../iframe.html'
will be displayed, then a script inside the iframe
will detect if the iframe is being displayed in URL //www.../exemplo.html
. If yes, it will execute a command, if not, it will execute another command.
Sample page: https://editor.sollic.com/stackoverflow/exemplo
Src of the iframe: https://editor.sollic.com/stackoverflow/iframe
Example script to identify if the page is being displayed in an iframe:
window.onload = function iframe(){
var frame = window.frameElement;
var origem = //identificar onde o iframe está sendo exibido
if(frame){
if(origem != "https://editor.sollic.com/stackoverflow/exemplo.html"){
//código aqui
}else{
//código aqui
}
}
}
Example iframe:
<iframe src="//editor.sollic.com/stackoverflow/iframe"></iframe>
Your question is a little vague. I did not quite get what you want to do... Could you try to clarify a little more please?
– Luiz Felipe
@Luizfelipe changed the question
– Mark Vaaz
https://stackoverflow.com/questions/935127/how-to-access-parent-iframe-from-javascript
– DaviAragao
from what I understand you want to know if a page of yours is being used as an iframe on any other page, and what is the url of that external page is this?
– Ricardo Pontual
That’s right, if I use
parent
it returns the url, but if iframe is inside another iframe, it will return the url of the parent iframe and not the one of the page.– Mark Vaaz
Try Document.referrer
– GilCarvalhoDev