【问题标题】:location.href = 'URL' redirects to a new page not a new website [duplicate]location.href ='URL'重定向到新页面而不是新网站[重复]
【发布时间】:2021-12-22 08:08:06
【问题描述】:

我正在尝试制作一个涉及重定向到另一个网站的项目。我在这样做时遇到了麻烦。

这是我的代码

document.getElementById("ContinueToWeb").onclick = function () {
   location.href = "google.com";
}
<html>
  <head>
    <!--Some code here-->
  </head>
  <body>
    <!--Some code here-->
    <button id="ContinueToWeb">Continue</button>
  </body>
</html>

由于某种原因,它从c:/folder/index.html 重定向到c:/folder/google.com,即使我告诉它从c:/folder/index.htmlgoogle.com。你知道发生了什么吗?

【问题讨论】:

  • 把网址改成https://google.com
  • 如果你告诉它转到index2.html,你会希望它转到http://index2.html吗?
  • 是的,我希望它会转到HTTP://index2.html

标签: javascript html


【解决方案1】:

您必须为链接添加协议,否则它默认为站点正在使用的本地文件系统。请改用https://www.google.com

【讨论】:

  • 成功了,谢谢
猜你喜欢
  • 2020-04-22
  • 2019-03-10
  • 1970-01-01
  • 2020-02-20
  • 1970-01-01
  • 2012-07-07
  • 1970-01-01
  • 2011-11-19
相关资源
最近更新 更多