Call functions on pages from background

穿精又带淫゛_ 提交于 2019-12-13 08:52:01

问题


Is it possible to call function on a page?

For example I have a page called help.html and that page has a function called help(id).

Can I call this function and pass the id to it?

Thanks in advance


回答1:


You can if the page is opened and loaded, and you have a reference to its window object. One way to make sure both things are true is to have code on your help window call a function on your background page when it is ready, passing a reference to itself. So on your help window you can have something like:

chrome.extension.getBackgroundPage().helpWindowLoaded(window);

And on your background page:

function helpWindowLoaded(helpWindow) {
    helpWindow.help(id);
}



回答2:


I am not sure I understand your question, but it will be better create a separate js-file. Here is an example (I use jQuery):

popup.html:

<html>
    <head>
        <script src="jquery-2.0.3.js"></script>
        <script src="popup.js"></script>
    </head>
    <body>
        <input id="amount" type="text" />
        <input id="addAmount" type="submit" value="Add" />
    </body>
</html>

popup.js:

$(function() {     
    $('#addAmount').click(function() {
       alert('hi');
    });
});


来源:https://stackoverflow.com/questions/19749425/call-functions-on-pages-from-background

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!