Detect the URL where iframe is being displayed

Asked

Viewed 244 times

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.

  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?

  • @Luizfelipe changed the question

  • 1

    https://stackoverflow.com/questions/935127/how-to-access-parent-iframe-from-javascript

  • 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?

  • 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.

  • Try Document.referrer

Show 1 more comment

1 answer

4


Use:

top.window.document.location.href

Will return the document URL more high, 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)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.