首页 > 解决方案 > 使用 JavaScript 后 URL 未加载

问题描述

以下代码应在 Web 浏览器中打开给定的 URL。但是单击按钮后它不起作用。

<!DOCTYPE html>
<html>

<head>
  <title></title>
</head>

<body>
  <input id="b">

  <button onclick="a()">Search</button>
  <script type="text/javascript">
    function a(value) {
      var x = document.getElementById("b").value;
      var url = "";
      if (x.startsWith("http://")) {
        url = x;
      } else {
        url = "http://" + x;
      }
      window.open(url);
    }
  </script>
</body>

</html>

标签: javascripthtml

解决方案


你可以试试这个

<!DOCTYPE html>
<html>

<head>
  <title></title>
</head>

<body>
  <input id="b">

  <button onclick="a()">Search</button>
  <script type="text/javascript">
    function a(value) {
      var x = document.getElementById("b").value;
      var url = "";
      if (x !="") {
        url = x;
      }
      window.open("http://"+url);
    }
  </script>
</body>

</html>

这是代码链接 https://codepen.io/anon/pen/qedoRZ

您可以填写google.com输入框然后打开goole主页。


推荐阅读