1

I have a script which I need to change at runtime and reload changes

So the script is:

<body>
<script id="scriptId" src="myJsFile_1.js"></script>

//Rest of the page here


<script>

$(document).ready(function() {

    $("#buttonId").click(function() {

        //Change the script src to myJsFile_2.js and reload

    })

});

</script>

</body>
</html>

How can I do this?

2
  • Possible duplicate of javascript jquery change src of a script using a script Commented Oct 27, 2016 at 13:32
  • What do you mean by reload? If you reload the page, myJsFile_1.js will be the file that is used every time. If you aren't talking about reloading the page, then why not just perform the view changes to the DOM within myJsFile_1.js when the user clicks the button? Commented Oct 27, 2016 at 13:40

1 Answer 1

0

use this

$(document).ready(function() {
    $("#buttonId").click(function() {

        $(document.body).remove('#scriptId').append('<script id="scriptId" type="text/javascript" src="your_script_src"></script>');

    });
});
Sign up to request clarification or add additional context in comments.

1 Comment

Got problems with the closing < in </script> ...says unclosed string literal

Your Answer

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