I am creating an extension for Chrome. I want to execute a script when the user clicks a button. Here is the code i currently have in my 开发者_JAVA技巧html file
<input type="button" id ="button" onclick='notEmpty()' value="Play"/>
The notEmpty()
is in a javascript (.js) file. How can I execute another script in the notEmpty()
function?
I have not written an extension for chrome but for typical javascript you can do one of the following.
<script type="text\javascript">
function notEmpty()
{
someOtherFunction();
}
</script>
or
<input type="button" id ="button" onclick='notEmpty(); someOtherFunction();' value="Play"/>
If you want to use functions that are located in another file, you would need to import those files:
<script type="text/javascript" src="/path/to/script.js"></script>
But, in Google Chrome Extensions, if you want to "execute" a script when you click on a button (from an extension page), you can use the chrome.tabs.executeScript functionality
// Run that script on the current tab. The first argument is the tab id.
chrome.tabs.executeScript(null, {file: '/path/to/script.js'});
It all depends on your scenario, when you "executeScript" as shown above, it will inject a content script into that DOM. If you import that script, you will just use those functionality in the current DOM (in that case the extension page, not the tab).
Hope that helped!
精彩评论