[英]Call javascript file and function, when an image is clicked
I am trying to run an external script that I cannot edit. 我正在尝试运行无法编辑的外部脚本。 Currently I am calling upon the script when the page loads.
当前,我正在页面加载时调用脚本。 This slows my site down.
这会使我的网站变慢。 I would like to instead call the javascript file and run the function when it is clicked.
我想改为单击该javascript文件并运行该函数。 I made it a little faster by calling the function, when clicked.
我通过单击该函数来使其速度更快。 However, it still calls the javascript file because I don't know how to make it call that and the function, when clicked.
但是,它仍然调用javascript文件,因为单击时我不知道如何调用它和该函数。 I spent a few hours reading and trying to find a similar request on Google with no success.
我花了几个小时阅读并试图在Google上找到类似的请求,但没有成功。
Below you will see what I have came up with so far. 在下面,您将看到我到目前为止提出的内容。
<div id="myFunctionID" style="background: url('http://example.com/button.png') repeat
scroll 0 0 transparent; width: 100px; height: 50px; cursor: pointer; position:fixed;
bottom:0; left:0;">
</div>
<script type="text/javascript" src="https://externalScript.js"></script>
Please help me load the JS
file and the myFunctionID
, when the button image is clicked. 单击按钮图像时,请帮助我加载
JS
文件和myFunctionID
。
Using javascript (Quoted from JavaScript - function to load external JS files is needed ) 使用javascript(引自JavaScript-需要加载外部JS文件的功能 )
function loadScript(url, callback){
var script = document.createElement("script")
script.type = "text/javascript";
if (script.readyState){ //IE
script.onreadystatechange = function(){
if (script.readyState == "loaded" ||
script.readyState == "complete"){
script.onreadystatechange = null;
callback();
}
};
} else { //Others
script.onload = function(){
callback();
};
}
script.src = url;
document.getElementsByTagName("head")[0].appendChild(script);
}
Using jquery 使用jQuery
$("button").click(function(){
$.getScript("pathto/external.js");
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.