Home >Web Front-end >JS Tutorial >How can I redirect the parent window from an iFrame using JavaScript?

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

DDD
DDDOriginal
2024-10-26 18:11:30819browse

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>
  • Redirect Parent IFrame:

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

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>

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!

Statement:
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