Chrome扩展程序未加载EventListener [英] Chrome extension not loading EventListener

查看:96
本文介绍了Chrome扩展程序未加载EventListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在创建chrome扩展程序,它将我的新标签页分成2个框架,因此我可以尝试加载2个不同的URL.现在,我从简单开始,但无法按预期加载它们.这是代码:

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. Right now I've started off simple, but I can't get them to load as intended. Here's the code:

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>

左右

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

Script.js

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

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

我尝试在帧之前和之后以及每个帧内部加载js.当我单击输入按钮时,什么也没有发生.我检查了该页面,它没有显示任何事件侦听器,并且在控制台中没有错误.但是,如果我尝试切出 frameset 并尝试仅用一个按钮加载页面,则会收到一个错误,该错误显示为 Uncaught SyntaxError:Unexpected token( for the line 2 of script.js .我缺少什么吗?

I've tried loading the js before and after the frames, and also inside each frame. 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.

推荐答案

将此脚本从Bakcground.html移至Left@Right.html

Move this script from Bakcground.html to Left@Right.html

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

然后在script.js中删除."点击之前

And then in the script.js remove the "." before click

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

上面的行应该是

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

这篇关于Chrome扩展程序未加载EventListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆