如何在用户点击按钮时执行javascript [英] How can i execute a javascript when a user clicks a button

查看:195
本文介绍了如何在用户点击按钮时执行javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为Chrome创建一个扩展程序。我想在用户单击按钮时执行脚本。这里是我目前在我的html文件中的代码

 < input type =buttonid =buttononclick = 'notEmpty()'value =播放/> 

notEmpty()位于javascript (.js)文件。我怎样才能在 notEmpty()函数中执行另一个脚本?

解决方案

如果您想使用位于另一个文件中的函数,则需要导入这些文件:

 <脚本类型=text / javascriptsrc =/ path / to / script.js>< / script> 

但是,在Google Chrome浏览器扩展程序中,如果您希望在执行脚本时点击按钮(从扩展页面),您可以使用 chrome.tabs.executeScript 功能

  //在当前选项卡上运行该脚本。第一个参数是标签ID。 
chrome.tabs.executeScript(null,{file:'/path/to/script.js'});

这一切都取决于你的场景,当你如上所示的executeScript时,它会注入一个<一个href =http://code.google.com/chrome/extensions/content_scripts.html =nofollow>内容脚本到该DOM中。如果您导入该脚本,您只需在当前DOM中使用这些功能(在这种情况下是扩展页面,而不是标签)。



希望这有帮助! p>

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 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?

解决方案

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!

这篇关于如何在用户点击按钮时执行javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆