EASY - please help (change .js src to another .js src onclick) how?

Tried to do this with getElementById and script#js but can't...

Page 1 of 1

3 Replies - 1179 Views - Last Post: 15 March 2010 - 12:34 PM

#1 thefreemenucom  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 26-January 10

EASY - please help (change .js src to another .js src onclick) how?

Posted 13 March 2010 - 04:02 PM

Let me start off by saying I'm very new to scripting languages but so far I love them all and I'm learning multiple new languages right now. I've built a website that is almost all contained in a .js file and although I now understand how dumb that was it would take a much deeper knowledge of html and javascript in order to remake it in a better way.

skip to problem

<script type="text/javascript" src="js/SetAllVarsBlank.js"></script>
<script id='blankjs' type="text/javascript" src="js/blank.js"></script>
<script type="text/javascript" src="js/StringSite.js"></script>


I need to change

<script id='blankjs' type="text/javascript" src="js/blank.js"></script>


to

<script id='blankjs' type="text/javascript" src="js/A_different_js_file.js"></script>


using onclick.

Any help would be greatly appreciated... I've already spent 3 hours looking up how to getElementById only to fail miserably. I'm not an idiot I just know almost no javascript and only have been using it to document.write html code that is repetitive and needs variables.

This post has been edited by thefreemenucom: 13 March 2010 - 04:03 PM


Is This A Good Question/Topic? 0
  • +

Replies To: EASY - please help (change .js src to another .js src onclick) how?

#2 BetaWar  Icon User is online

  • #include "soul.h"
  • member icon

Reputation: 1167
  • View blog
  • Posts: 7,207
  • Joined: 07-September 06

Re: EASY - please help (change .js src to another .js src onclick) how?

Posted 14 March 2010 - 01:56 PM

Sadly you can't use getElementById on script tags since they don't support the ID attribute:
http://www.w3schools.../tag_script.asp

You could do something like so though (I believe):
var scripts = document.getElementsByTagName("SCRIPT");
var blank_js_script = scripts[1];
blank_js_script.src = "http://www.w3schools.com/TAGS/tag_script.asp";



NOTE - Untested code.

Hope that helps.
Was This Post Helpful? 1
  • +
  • -

#3 thefreemenucom  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 26-January 10

Re: EASY - please help (change .js src to another .js src onclick) how?

Posted 14 March 2010 - 03:15 PM

I really appreciate your help, I tested that a few ways and couldn't get it to work, it has no effect and won't load a js file to any script position. Anyways, I thought that even though it wasn't supported that it would still have a chance of working : \, this is the last thing I need to figure out before my site will be usable. I think I can just contain the .js contents in a variable and then call that variable using a function ... haha, I'm a complete newb, I'm studing though, just paid lynda almost 400 dollars so that I'll be able to use their site for a year, starting college asap when I get out of the military.
Was This Post Helpful? 0
  • +
  • -

#4 thefreemenucom  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 26-January 10

Re: EASY - please help (change .js src to another .js src onclick) how?

Posted 15 March 2010 - 12:34 PM

View Postthefreemenucom, on 13 March 2010 - 03:02 PM, said:

Let me start off by saying I'm very new to scripting languages but so far I love them all and I'm learning multiple new languages right now. I've built a website that is almost all contained in a .js file and although I now understand how dumb that was it would take a much deeper knowledge of html and javascript in order to remake it in a better way.

skip to problem

<script type="text/javascript" src="js/SetAllVarsBlank.js"></script>
<script id='blankjs' type="text/javascript" src="js/blank.js"></script>
<script type="text/javascript" src="js/StringSite.js"></script>


I need to change

<script id='blankjs' type="text/javascript" src="js/blank.js"></script>


to

<script id='blankjs' type="text/javascript" src="js/A_different_js_file.js"></script>


using onclick.

Any help would be greatly appreciated... I've already spent 3 hours looking up how to getElementById only to fail miserably. I'm not an idiot I just know almost no javascript and only have been using it to document.write html code that is repetitive and needs variables.


Edit- Found a way but there is a problem, scripts afterwards don't take into account variables on .js file being added

function createjscssfile(filename, filetype){
 if (filetype=="js"){ //if filename is a external Javascript file
  var fileref=document.createElement('script')
  fileref.setAttribute("type","text/javascript")
  fileref.setAttribute("src", filename)
 }
 else if (filetype=="css"){ //if filename is an external CSS file
  var fileref=document.createElement("link")
  fileref.setAttribute("rel", "stylesheet")
  fileref.setAttribute("type", "text/css")
  fileref.setAttribute("href", filename)
 }
 return fileref
}

function replacejscssfile(oldfilename, newfilename, filetype){
 var targetelement=(filetype=="js")? "script" : (filetype=="css")? "link" : "none" //determine element type to create nodelist using
 var targetattr=(filetype=="js")? "src" : (filetype=="css")? "href" : "none" //determine corresponding attribute to test for
 var allsuspects=document.getElementsByTagName(targetelement)
 for (var i=allsuspects.length; i>=0; i--){ //search backwards within nodelist for matching elements to remove
  if (allsuspects[i] && allsuspects[i].getAttribute(targetattr)!=null && allsuspects[i].getAttribute(targetattr).indexOf(oldfilename)!=-1){
   var newelement=createjscssfile(newfilename, filetype)
   allsuspects[i].parentNode.replaceChild(newelement, allsuspects[i])
  }
 }
}

replacejscssfile("oldscript.js", "newscript.js", "js") //Replace all occurences of "oldscript.js" with "newscript.js"
replacejscssfile("oldstyle.css", "newstyle", "css") //Replace all occurences "oldstyle.css" with "newstyle.css"

This post has been edited by thefreemenucom: 15 March 2010 - 12:35 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1