[英]javascript access web-based script on button click
How would I go about accessing a web-based script. 我将如何访问基于Web的脚本。 I don't know if I'm wording that correctly. 我不知道我的措词是否正确。 I just want to be able to click a button and run the function. 我只希望能够单击一个按钮并运行该功能。 That's not the issue. 那不是问题。 The issue is how do I go about putting it in a function. 问题是如何将其放入函数中。
<script type="text/javascript" src="http://wwww.blah.com/blah?blah=blah"></script>
how would i get that to run on button click? 我如何才能在单击按钮时运行它? I've tried every which way to put it in a function and call the function. 我尝试了各种方法将其放入函数中并调用该函数。 No luck though. 不过没有运气。
var buildScript = function (url, optionalCallback) {
var script = document.createElement("script"),
parent = document.getElementsByTagName("script")[0].parentElement;
script.onload = optionalCallback;
script.src = url;
parent.appendChild(script);
};
var button = document.getElementById("my-button");
button.onclick = function () { buildScript("//url.to.site/file.js"); };
Or optionally: 或可选:
button.onclick = function () {
buildScript("//url.to.site/file.js", function () {
function_from_loaded_script();
});
};
If you need it to do other stuff, you might need to be more specific. 如果您需要它来做其他事情,则可能需要更具体。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.