我想打开google.com或用户在窗口中键入的任何其他网站

时间:2019-05-21 09:10:33

标签: javascript html

我有以下代码可以打开用户输入 但问题是 它像打开 127.0.0.1:8887/www.google.com

我应该怎么做才能直接打开 www.google.com

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
<style type="text/css">
label {
display:block;
}
</style>
<script
src="https://code.jquery.com/jquery-3.4.1.slim.js"
integrity="sha256-BTlTdQO9/fascB1drekrDVkaKd9PkwBymMlHOiG+qLI="
 crossorigin="anonymous"></script>
<script type="text/javascript">

function goTo() {
    var url = document.forms[0].url.value;
    myWindow = window.open(url, "", "width=800,height=800");
    myWindow.focus();
    return false;
}

$('.url').keydown(function (e) {
    if (e.keyCode === 13) {
         e.preventDefault();
        goto();
    }
});

</script>
</head>
<body>
<form action="" method="get" onsubmit="return goTo()">
    <label for="url">Enter the URL:
    <input type="text" name="url" id="url">
    <input type="submit" value="Submit">
</form>
</body>
</html>

5 个答案:

答案 0 :(得分:1)

您需要在网址中输入协议信息。

您可以检查用户是否已经输入http://https://,如果没有输入,请添加//

url = /^https?:\/\//i.test(url) ? url : '//' + url
myWindow = window.open(url, "", "width=800,height=800");

答案 1 :(得分:1)

    function goTo() {
        var url = 'http://'+document.forms[0].url.value;
        myWindow = window.open(url, "", "width=800,height=800");
        myWindow.focus();
        return false;
    }

答案 2 :(得分:0)

检查用户是否输入https://,否则将其添加到输入的URL

答案 3 :(得分:0)

您需要添加协议(例如,将httpshttp添加到URL,以使其知道它不是子URL)。像这样尝试:

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
<style type="text/css">
label {
display:block;
}
</style>
<script
src="https://code.jquery.com/jquery-3.4.1.slim.js"
integrity="sha256-BTlTdQO9/fascB1drekrDVkaKd9PkwBymMlHOiG+qLI="
 crossorigin="anonymous"></script>
<script type="text/javascript">

function goTo() {
    var url = document.forms[0].url.value;
    if(!(url.startsWith("http://") || url.startsWith("https://")))
        url = "//"+url; //<-- Adds protocol

    myWindow = window.open(url, "", "width=800,height=800"); 
    myWindow.focus();
    return false;
}

$('.url').keydown(function (e) {
    if (e.keyCode === 13) {
         e.preventDefault();
        goto();
    }
});

</script>
</head>
<body>
<form action="" method="get" onsubmit="return goTo()">
    <label for="url">Enter the URL:
    <input type="text" name="url" id="url">
    <input type="submit" value="Submit">
</form>
</body>
</html>

答案 4 :(得分:-1)

    <!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
<style type="text/css">
label {
display:block;
}
</style>
<script
src="https://code.jquery.com/jquery-3.4.1.slim.js"
integrity="sha256-BTlTdQO9/fascB1drekrDVkaKd9PkwBymMlHOiG+qLI="
 crossorigin="anonymous"></script>
<script type="text/javascript">

function goTo() {
    var url = "http://"+document.forms[0].url.value;
    myWindow = window.open(url, "", "width=800,height=800");
    myWindow.focus();
    return false;
}

$('.url').keydown(function (e) {
    if (e.keyCode === 13) {
         e.preventDefault();
        goto();
    }
});

</script>
</head>
<body>
<form action="" method="get" onsubmit="return goTo()">
    <label for="url">Enter the URL:
    <input type="text" name="url" id="url">
    <input type="submit" value="Submit">
</form>
</body>
</html>