How to use the document.referrer Property In Javascript
The document.referrer property returns the URI of the page that linked to the current page. By checking if the referrer is empty or not, we can determine if the page was loaded directly or within an iFrame. If the document.referrer is not empty, it likely indicates that the page is within an iFrame.
Example: In this example, we are using the document.referrer property to check if the webpage is in an iFrame.
// Function to check whether webpage is in an iFrame
function iniFrame() {
if (document.referrer !== '') {
// The page is in an iFrame
document.write("The page is in an iFrame");
} else {
// The page is not in an iFrame
document.write("The page is not in an iFrame");
}
}
// Calling iniFrame function
iniFrame();
Output:
The page is in an iFrame
How to check a webpage is loaded inside an iframe or into the browser window using JavaScript?
An iFrame is a rectangular frame or region in the webpage to load or display another separate webpage or document inside it. So basically, an iFrame is used to display a webpage within a webpage. You can see more about iFrames here : HTML iFrames There may be a variety of reasons for checking whether a webpage is loaded in an iFrame, for example, in cases where we need to dynamically adjust the height or width of an element.
These are the following methods:
Table of Content
- Comparing the object’s location with the window object’s parent location
- Using window.top and window.self property
- Using the window.frameElement property
- Using the document.referrer Property