Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

170
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda