Question:
How to detect the URL where the iframe
is being displayed and execute a function if the page is being displayed at a specific URL.
Example: when accessing the page https://www.../exemplo.html
(not necessarily from the same domain) an iframe
with src='//www.../iframe.html'
will be displayed, then a script inside the iframe
will detect if the iframe is being displayed at the URL //www.../exemplo.html
. If yes, it will execute a command, if not, it will execute another command.
-
Example page: https://editor.sollic.com/stackoverflow/example
-
iframe src: 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>
Answer:
Use:
top.window.document.location.href
Will return the highest document URL regardless of which iframe
you call.
For example:
http://site.com/index.html
|
----> iframe1.html (retorna http://site.com/index.html)
|
----> iframe2.html (retorna http://site.com/index.html)
|
----> iframe3.html (retorna http://site.com/index.html)