搜索谷歌的输入

时间:2018-05-23 18:26:45

标签: javascript html google-chrome-extension google-chrome-theme

我正在为我的Chromebook制作一个扩展主题,用于搜索编码网站(例如此网站,w3schools等)。我该如何制作?到目前为止,这是我的代码:

<html>
<head>
</head>
<body>    
<input id="input1">
<button onclick="searchGoogle()">Search Google</button>
<script>
function searchGoogle() {
        var one = document.getElementById("input1").value;
        var two = 'http://www.google.com/search?q=' + one;
        window.location = two;
    }
</script>
</body>
</html>

我的代码不起作用

运行时会弹出:

(Image of my code running)

我的代码错了吗?

任何帮助都会受到重视。

修改

<html>
<head>
<script src="searchgoogle.js">
</script>
</head>
<body>    
<input id="input1">
<button id="link">Search Google</button>

</body>
</html>

document.addEventListener('DOMContentLoaded', function() {
    var link = document.getElementById('link');
    // onClick's logic below:
    link.addEventListener('click', function() {

        function searchGoogle() {
        var one = document.getElementById("input1").value;
        var two = 'https://www.google.com/search?q=' + one;
        window.location = two;
    }


    });
});

无法正常工作

1 个答案:

答案 0 :(得分:1)

在侦听器函数中声明函数searchGoogle,但从不调用它。试试:

document.addEventListener('DOMContentLoaded', function() {
    var link = document.getElementById('link');
    // onClick's logic below:
    link.addEventListener('click', function() {

        //there is no need to declare a new function here.

        var one = document.getElementById("input1").value;
        var two = 'https://www.google.com/search?q=' + encodeURIComponent(one);
        window.location = two;



    });
});