javascript – Detect the URL from where the iframe is being displayed


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.

  window.onload = function iframe(){
    var frame = window.frameElement;
    var origem = //identificar onde o iframe está sendo exibido
      if(origem != ""){
        //código aqui
        //código aqui

Example iframe:

<iframe src="//"></iframe>




Will return the highest document URL regardless of which iframe you call.

For example:
     ----> iframe1.html (retorna
                ----> iframe2.html (retorna
                           ----> iframe3.html (retorna
Scroll to Top