Home > Web Front-end > JS Tutorial > body text

How can I redirect the parent window from an iFrame using JavaScript?

DDD
Release: 2024-10-26 18:11:30
Original
749 people have browsed it

How can I redirect the parent window from an iFrame using JavaScript?

Redirecting Parent Window from an iFrame

In the presence of an iFrame embedded within a parent window, the need may arise to redirect the parent window's location to a new URL. To achieve this, JavaScript offers a simple solution.

Using JavaScript to Redirect the Parent Window

Within the JavaScript code of your iFrame, you can utilize the following methods:

  • Redirect Top-Most Parent IFrame:

    <code class="javascript">window.top.location.href = "http://www.example.com";</code>
    Copy after login
  • Redirect Parent IFrame:

    <code class="javascript">window.parent.location.href = "http://www.example.com";</code>
    Copy after login

These code snippets will instruct the respective parent iFrame (top-most or immediate) to redirect to the specified URL.

Example HTML for IFrame with Hyperlink

<code class="html"><iframe src="iframe.html"></iframe>

<a href="#" onclick="window.parent.location.href = 'http://www.example.com'">Click Here to Redirect Parent IFrame</a></code>
Copy after login

By clicking the hyperlink within the iFrame, the parent window will be redirected to the specified URL.

The above is the detailed content of How can I redirect the parent window from an iFrame using JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!