-2

I fire my javascript using the onload property -> window.onload = function(){ // start code }

Rececently I started doing an .innerHTML write to the <head> tag which sets my .js file.

document.head.innerHTML = html_string

// where html_string contains the link to my .js file.

Problem I'm having is that now the onload function is never called so my page has no javascript on it.

Is there a way to manually call it or emulate its behavior?

CS_2013
  • 1,158
  • 3
  • 13
  • 24
  • why would you want this? Why nou write the link? – 11684 May 14 '12 at 17:45
  • Why did you do that? It worked fine before the change, it doesn't work after the change, did I hear **rollback**? – gdoron May 14 '12 at 17:45
  • One guess it that .innerHTML writes have no effect on what the browser would call a load event....the first thing I could try is simply call the function at the top of my code in place of setting the property...but manually firing it by placing some embedded code at the end of the document would make more sense. – CS_2013 May 14 '12 at 17:47
  • **It seems like you know the problem and you know how to solve it. What is your question??** – gdoron May 14 '12 at 17:49
  • Well I guess i answered it myself...just call the function at the bottom of the html document in embedded form...this will ensure the dom is loaded...should emulate it perfectly..with one line of code. – CS_2013 May 14 '12 at 17:51
  • this does not work for some reason. – CS_2013 May 14 '12 at 18:05
  • the inserted embedded javascript does not run either. – CS_2013 May 14 '12 at 18:06

1 Answers1

1

Don't insert the scripts by setting the innerHTML of document.head. Add a <script> DOM node and listen for its load event: https://stackoverflow.com/a/9102718/1048572, Capturing onload event when dynamically loading .js files?, jQuery event that triggers after CSS is loaded?

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375