0

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

John
  • 1,095
  • 3
  • 15
  • 31

2 Answers2

0

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');
    });
});
ceth
  • 44,198
  • 62
  • 180
  • 289
0

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);
}
rsanchez
  • 14,467
  • 1
  • 35
  • 46
  • Use [`chrome.runtime.getBackgroundPage(callback)`](https://developer.chrome.com/extensions/runtime.html#method-getBackgroundPage) for [event pages](https://developer.chrome.com/extensions/event_pages.html). To literally answer the question ("Call functions on pages *from background*"), see [`chrome.extension.getViews`](https://developer.chrome.com/extensions/extension.html#method-getViews) – Rob W Nov 21 '13 at 14:52