简体   繁体   English

Chrome扩展程序未加载EventListener

[英]Chrome extension not loading EventListener

I've been creating a chrome extension that splits my new tab pages into 2 frames so I can try to load 2 different URLs. 我一直在创建一个chrome扩展程序,它将我的新标签页分成2个框架,因此我可以尝试加载2个不同的URL。 Right now I've started off simple, but I can't get them to load as intended. 现在,我从简单开始,但是无法按预期加载它们。 Here's the code: 这是代码:

Background.html Background.html

<!DOCTYPE html>
<html> 
    <head>   
        <title>2 Pages</title>
    </head>     
    <frameset cols="*,*">
        <frame src="left.html" name="left">
        <frame src="right.html" name="right">
    </frameset>     
    <script type="text/javascript" src="script.js"></script>    
</html>

Left and Right 左和右

<!DOCTYPE html>
<html>  
    <body>   
        <input type="button" value="Load new document" id="loader">
    </body> 
</html>

Script.js Script.js

document.addEventListener('DOMContentLoaded', function() {
  document.getElementById("loader").addEventListener.("click", loadUrl);
});

function loadUrl(){
    window.location = 'http://www.w3schools.com';
    return false;
}

I've tried loading the js before and after the frames, and also inside each frame. 我试过在帧之前和之后以及每个帧内加载js。 when I click the input button, nothing happens. 当我单击输入按钮时,没有任何反应。 I inspected the page, and it shows no event listeners present, and no errors in the console. 我检查了该页面,它没有显示任何事件侦听器,并且在控制台中没有错误。 Though, If I try to cut out the frameset and try just loading a page with one button, I get an error that reads Uncaught SyntaxError: Unexpected token ( for line 2 of script.js . Is there something I'm missing? Thanks. 但是,如果我尝试切出frameset并尝试仅用一个按钮加载页面,则会收到一个错误消息,内容为Uncaught SyntaxError: Unexpected token (对于script.js第2行。我缺少什么吗?)。

Move this script from Bakcground.html to Left@Right.html 将此脚本从Bakcground.html移到Left@Right.html

<script type="text/javascript" src="script.js"></script> 

And then in the script.js remove the "." 然后在script.js中删除“。” before click 点击之前

document.getElementById("loader").addEventListener.("click", loadUrl);

the above line should be 上面的行应该是

document.getElementById("loader").addEventListener("click", loadUrl);

In your manifest.json file, you should add a key, that indicates whenever to run your .js scripts. 在manifest.json文件中,您应该添加一个密钥,该密钥指示何时运行.js脚本。

You should place it under "content_scripts" as following: 您应将其放在“ content_scripts”下,如下所示:

{
...
    "content_scripts": [{
        "run_at": "document_end",
...

"document_end" indicates that your code should run after the document, of the current tab, was loaded. “ document_end”表示代码应在加载当前选项卡的文档之后运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM