0

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. 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. I spent a few hours reading and trying to find a similar request on Google with no success.

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.

4
  • 1
    You will have to have your own .js file with a click handler that dynamically loads the externalScript.js and calls the appropriate function within it. Commented Apr 22, 2014 at 23:06
  • studying click handler now. Will post how I did it, if I can figure it out. Thanks Commented Apr 22, 2014 at 23:15
  • It is a bad idea to load the JS file each time the image button is clicked. Commented Apr 22, 2014 at 23:19
  • Why is it a bad idea to load the JS file, when a button is clicked. Only 1 out of 10,000 hits actually use the plugin. The plugin slows down my site; 76/100 pagespeed with the plugin and 84/100 without it per Google. Commented Apr 23, 2014 at 1:53

1 Answer 1

1

Using javascript (Quoted from JavaScript - function to load external JS files is needed)

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

$("button").click(function(){
  $.getScript("pathto/external.js");
}); 
Sign up to request clarification or add additional context in comments.

3 Comments

Just wanted to note that the question doesn't state the tag jquery. Also I would recommend if you want to use this, using the one function.
Seems like more is going on than needed. I will try to tackle the jQuery one tomorrow. Never got anything to work right for me, but it is due to my own JS ignorance.
Neither the JS nor the jquery worked for me. I have no errors in the console, but when clicking it doesn't load the external script. I also changed the ID of my background img div to be button. I tried with and without the # in front on the jquery too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.