How Can I Replace a Script Element?

I want to remove a script element and replace it with a different one. Lets say that you

<html> <head> <script src="default.js"> </head>

What I want to do is remove default.js and replace it with new.js on the click of a button. I know you can create a script like this:

var new_scr=document.createElement('script'); new_scr.src="new.js"; document.getElementsByTagName('head')[0].appendChild(new_scr);


you can set the id for script tag and change the content based on the id instead of replacing it with new one.

or following might be useful code block for you.

var $jsRef = $('<script type="text/javascript" >').appendTo(document.body); $jsRef.attr("src","jsFileName.js"); // You can also add content like this $jsRef.append(content); // You can change the js file name or content the same way $jsRef.attr("src","newJSFileName.js"); // Changing the content like this $jsRef.empty(); $jsRef.append(content);


I don't think you can "replace" it. The code will be parsed on page load, before your javascript has a chance to replace it. Therefore, even if you load something else, that javascript will already be running, i.e. in memory. I may be offbase, but I don't think you can "replace" a script unless you actually overwrite the items it established.


  • Plotting multiple maps with ggmap
  • Kendo UI Grid Data variable Vue.js
  • Dynamically created HTML5 audio is not playable in some browsers?
  • Why is document.execCommand(“copy”) no longer working in Internet Explorer 11?
  • php echo first divs
  • cannot play iPad over https
  • Javascript event doesn't seem to get added to a dynamically generated textbox
  • jquery getting the html of highlighted text and changing its style
  • javascript jquery change src of a script using a script
  • html2canvas creates a canvas with a width and height of zero?
  • Select element inside WebView and get details
  • Javascript get anchor href on click
  • How to store value from one dropdown list to another dropdown list
  • ThreeJS - Intersection of a line and sphere
  • Replace Node Name
  • pure javascript dom dynamic insert, update and delete
  • Dom Node for PHP find href attribute issue
  • getElementsByClassName and AJAX
  • After converting my FBX file to a .gltf, the model is incredibly small, why?
  • react native create element with string
  • Can a variable be stored within an image or div tag?
  • how to remove a div with same ids but display='block' and display='none' in JAVa
  • Why does the following throw an “Object doesn't support property or method 'importNode
  • Getting error 'Cannot read property 'document' of undefined' while importing exp
  • Chart.js Multiple dataset
  • How do I get HTML corresponding to current DOM tree?
  • JQuery Internet Explorer and ajaxstop
  • JSON response opens as a file, but I can't access it with JavaScript
  • FileReader+canvas image loading problem
  • Can a Chrome extension content script make an jQuery AJAX request for an html file that is itself a
  • Change an a tag attribute in JavaScript based on screen width
  • Adding custom controls to a full screen movie
  • Comma separated Values
  • How to delete a row from a dynamic generate table using jquery?
  • Error creating VM instance in Google Compute Engine
  • using HTMLImports.whenReady not working in chrome
  • Hits per day in Google Big Query
  • how does django model after text[] in postgresql [duplicate]
  • Authorize attributes not working in MVC 4
  • EntityFramework adding new object to nested object collection