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

143
Visualizações
How can I grab a clicked word in an iframe src with js?

This js allows copying a clicked word in an html element to var "str". But I want to grab a word in an iframe src. I modified the js to target the iframe id #USCF_CUST and grab the clicked word, but it doesn't seem to work. The clicked word is not selected and copied to var "str". Can anyone tell me why?

<div class = "clickable">The rain in Spain falls mainly in the plane.</div>

 $(".clickable").click(function(e) {
 s = window.getSelection(); var range = s.getRangeAt(0); var node = s.anchorNode;
 while (range.toString().indexOf(' ') != 0) {range.setStart(node, (range.startOffset - 1));}
 range.setStart(node, range.startOffset + 1);
 do {range.setEnd(node, range.endOffset + 1);}
 while (range.toString().indexOf(' ') == -1 && range.toString().trim() != '');
 str = range.toString().trim(); alert (str);
});

But the above js doesn't work (it does nothing) in this instance:

<iframe id = 'USCF_CUST' src= url_2 ></iframe>

 $("#USCF_CUST").click(function(e) {
 s = window.getSelection(); var range = s.getRangeAt(0); var node = s.anchorNode;
 while (range.toString().indexOf(' ') != 0) {range.setStart(node, (range.startOffset - 1));}
 range.setStart(node, range.startOffset + 1);
 do {range.setEnd(node, range.endOffset + 1);}
 while (range.toString().indexOf(' ') == -1 && range.toString().trim() != '');
 str = range.toString().trim(); alert (str);
});
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

When you write window you reference the window where the code is executed. However, it seems like you want to reference iframes window.

Try using contentWindow property of iframe in order to set listeners, getSelection, etc.

It will only work if the domain of both pages is exactly the same.

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