开发者

How can i execute a javascript when a user clicks a button

开发者 https://www.devze.com 2023-02-01 18:58 出处:网络
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

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!

0

精彩评论

暂无评论...
验证码 换一张
取 消