首页 > 解决方案 > 如何在codeandbox.io 中使用window.location.href?

问题描述

我想window.location.href在工具codeandbox.io中使用。这是因为我想在发生硬页面加载的情况下进行测试。但是,我遇到了一个问题。

location.href = "http://www.google.com"

我收到此错误:

Mixed Content: The page at 'https://codesandbox.io/s/lingering-bird-nyvfi' was loaded over HTTPS, but requested an insecure resource 'http://www.google.com/'. This request has been blocked; the content must be served over HTTPS.

示例: https ://codesandbox.io/s/locationhref-usage-nyvfi (查看src/index.ts并打开开发控制台)

如何在假的虚拟浏览器中实现模拟页面加载的能力?

标签: window.locationcodesandbox

解决方案


I think it may be possible once the security criteria are met; which is limiting.

  • Use HTTPS (Avoid mixed content)
  • Use same origin

Thus this works:

window.location.href = "https://www.codesandbox.io/docs";

推荐阅读