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

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

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

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by lottie , 7 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.

Related Threads:

How to check the status code of url in iframe in react.js?
How to check if a mysql schema was changed?
How to get event when iframe src is changed in react.js?
How to detect when the browser blocks an iframe?
How to detect if browser support iframe?
How to check the upstream url in git?