Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

168
Views
Stop iframe from redirect to new window and replace its source

I hope I'll find a solution to my problem here. Suppose it is a code:

<iframe src="https://anyweb.ext/anymouse"></iframe>

As the page loads, it gets redirected to a new window. I want it not to be redirected, but the redirected URL should be replaced with iframe src="". I want to do it JavaScript.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Method-1

If you assign data-src to the src attribute of the <iframe> element using Javascript, the <iframe> will open on the same page.

const mainPageButton = document.getElementById('mainPageButton');
const talkPageButton = document.getElementById('talkPageButton');
const iframeElement = document.getElementById('specialIFrame');
const mainPage = "https://en.wikipedia.org/";

/* Opens the "data-src" attribute of the <iframe> element. */
mainPageButton.addEventListener('click', function(){
  iframeElement.setAttribute("src", mainPage)
});

/* Opens the address defined in the mainPage variable. */
talkPageButton.addEventListener('click', function(){
  iframeElement.setAttribute("src", iframeElement.dataset.src)
});
button { margin-bottom: 10px; margin-left: 75px; }
<button id="mainPageButton">OPEN Main Page</button>
<button id="talkPageButton">Open Talk Page</button><br>

<iframe id="specialIFrame" data-src="https://en.wikipedia.org/wiki/Talk:Main_Page" src="about:blank" width="500"></iframe>

Method-2

Adding the target attribute to the <a> element and the name attribute to the <iframe> element opens the website within the same page.

a{ display: block; }
<a target="myIframe" href="https://en.wikipedia.org/wiki/Main_Page">Open</a> 

<iframe name="myIframe"></iframe>

References
  • Open link in <iframe>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!