David
David

Reputation: 13999

How to check for script src match, then reassign src

I need to check every script tag's src value and if there is a match, I would like to change that script tags src attribute... Something like this:

var scripts = document.getElementsByTagName("script")[0].src

for (i=0;i<scripts.length;i++){
  if(scripts[i] == "something.js"){
    document.getElementsByTagName("script")[i].src = "this.js"
  }
  else {}
}} 

Upvotes: 3

Views: 6942

Answers (1)

&#193;lvaro Gonz&#225;lez
&#193;lvaro Gonz&#225;lez

Reputation: 146460

var scripts = document.getElementsByTagName("script");

for (i=0;i<scripts.length;i++){
  if(scripts[i].src == "something.js"){
    scripts[i].src = "this.js";
  }
}

Upvotes: 8

Related Questions