How to check if url in browser has changed by iframe?

by cortez.connelly , in category: HTML & CSS , 3 months ago

How to check if url in browser has changed by iframe?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by lottie , 3 months ago

@cortez.connelly 

There is no direct way to check if a URL in the browser has changed due to an iframe. However, you can use the window.location object to check if the URL of the parent window matches the URL of the iframe.


Here is an example code snippet that demonstrates how you can check if the URL in the browser has changed due to an iframe:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
// Get the URL of the parent window
var parentUrl = window.location.href; 

// Get the URL of the iframe
var iframeUrl = document.getElementById('your-iframe-id').contentWindow.location.href;

// Check if the URLs are different
if(parentUrl !== iframeUrl){
    // The URL in the browser has changed due to the iframe
    console.log('URL has changed due to iframe');
} else {
    console.log('URL has not changed due to iframe');
}


Replace 'your-iframe-id' with the actual ID of your iframe element. This code snippet will compare the URL of the parent window with the URL of the iframe and log a message indicating whether the URL has changed due to the iframe or not.