【问题标题】:Chrome extension won't run JS onClick [duplicate]Chrome 扩展程序不会运行 JS onClick [重复]
【发布时间】:2016-05-25 20:45:25
【问题描述】:

我正在尝试创建一个允许将两个 URL 输入到一个选项卡中的 chrome 扩展。我用frameset左右分开。代码如下:

Background.html

<!DOCTYPE html>
<html>     
    <head>     
        <title>A frame with two pages</title>    
    </head>
    <frameset cols="*,*">
        <frame src="left.html" name="left">
        <frame src="right.html" name="right">     
    </frameset>     
</html>

左右

<!DOCTYPE html>
<html>
<body onLoad="document.getElementById('urlToJump').value='http://www.foxnews.com/'">     
    <div class="menu">           
        <form>     
            <br>
            <input type="text" id="urlToJump">
            <br>     
            // Test 1 (custom input)
            <input type="button" id="restore" value="Go to the URL above" onclick="window.location.href=document.getElementById('urlToJump').value;" />     
            <br> 
            // Test 2 (calling function)
            <input type="button" value="Load new document" onclick="newDoc()">   
        </form>
    </div>
    <script>
        function newDoc() {
            window.location.assign("http://www.w3schools.com")
        }
    </script>
</body>     
</html>

两个测试都没有工作,因为没有加载预期的目标。如何解决?我正在尝试重新创建找到 here 的 Chrome 扩展程序,并添加更多内容。谢谢!

【问题讨论】:

    标签: javascript html google-chrome-extension


    【解决方案1】:

    这里的问题是 Chrome 扩展程序不允许您使用内联 JavaScript,这真的很糟糕。解决方案是为您的按钮提供一个 ID,然后使用 jQuery 或 JavaScript 选择您的按钮并在 JavaScript 文件中为其提供 onclick 属性。

    【讨论】:

    • 所以我只需要一个单独的JS文件?如果我没记错的话,我已经检查了可能的扩展。
    • 按惯例是的,但问题是您不能内联传递 onclick 属性。您需要在实际的 JavaScript 代码中传递它,至少我过去必须这样做。您可以在这篇文章中找到更多信息:developer.chrome.com/extensions/…
    • @Kragalon 它们可能是使用清单版本 1 的古老扩展。目前您无法开发使用旧约定的新扩展。
    • 是的。赞是对的。谢谢你,我希望我能帮上忙。当时,我不知道 MV1 中的内联 JavaScript 支持
    猜你喜欢
    • 1970-01-01
    • 2014-05-09
    • 2011-09-25
    • 1970-01-01
    • 2018-03-02
    • 1970-01-01
    • 2015-01-16
    • 1970-01-01
    • 2017-11-29
    相关资源
    最近更新 更多