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

117
Visualizações
Why sometimes it fails to use window.location.href

I just encounter weird and inconsistent behavior. I type below in w3c online tester.

test.htm

<!DOCTYPE html>
<html>
<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
<body>
<iframe onload="$.getScript('https://mysite.tw/test.js')">
</body>
</html>

where in test.js (on my server), I write:

alert('get!'); // this is just to confirm the js had indeed loaded
window.location.href="https://amazon.com";

Then the alert message jumps, but suddenly the browser blocks the redirect behavior, reporting the server refuses the access.

My first question is that why does this happen? I just want to direct to another site, not to make something complicate or sensitive, like Ajax, etc.

My second question is that I tried the similar code of test.htm on another site. And this time the page successfully redirect to amazon. Pretty weird. Is it due to the https problem? Or a cross-site security issue?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You test.js is in iframe, and you may check the response header from https://www.amazon.com/

x-frame-options: SAMEORIGIN

You cannot iframe the web page from other domain.

The X-Frame-Options HTTP response header can be used to indicate whether or not a browser should be allowed to render a page in a <frame>, <iframe>, <embed> or <object>. Sites can use this to avoid click-jacking attacks, by ensuring that their content is not embedded into other sites.

SAMEORIGIN The page can only be displayed in a frame on the same origin as the page itself.

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