.NET (290) administrative (42) Ajax (42) AngularJS (1) ASP.NET (144) bicycle (2) books (179) browser (8) C# (128) cars (1) chess (27) CodePlex (10) Coma (8) database (46) deployment (3) Entity Framework (2) essay (109) flash/shockwave (2) flex (1) food (3) friend (2) game (20) idea (5) IIS (8) javascript (81) LInQ (2) Linux (6) management (4) manga (43) misc (664) mobile (1) movies (88) MsAccess (1) murder (2) music (65) mysql (1) news (98) permanent (1) personal (66) PHP (1) physics (2) picture (309) places (12) politics (13) programming (495) rant (118) religion (3) science (40) Sharepoint (3) software (57) T4 (2) technology (11) Test Driven Development (4) translation (2) VB (2) video (100) Visual Studio (44) web design (45) Windows API (8) Windows Forms (3) Windows Server (4) WPF/Silverlight (63) XML (11)

Monday, January 07, 2008

Javascript external file not loading!!

Ok, so I used a javascript script in my page by referencing the external file and it worked. I did the exact same thing with another file and it wasn't loading! After scratching my head bald I've decided to switch the places of the two tags and voila! the script that worked would not load! The one that did not work previously was purring nicely.

My calls looked like this:
<script type="text/javascript" src='script1.js'/>
<script type="text/javascript" src='script2.js'/>


After scratching my skull a little more (blood was dripping already) I realized that the script tags are atomic tags, they should have no ending tag. Why would they, the content is specified in the src attribute. But on the DOM page for the script element there is an obscure line saying: Start tag: required, End tag: required. I switched to <script></script> format and it worked.

Oh, you are wondering why the first script worked? Because somehow an atomic script tag is erroneous, but it doesn't return any error. Instead it is treated like a mistyped start tag and the atomic portion of it is ignored. The second script would not load since the browser expected a script end tag. Maybe he even interpreted the second tag as an end tag for all I know.

8 comments:

Anonymous said...

I have been bleeding profusely for a number of hours before you figured out what was going on for me. Thank you very very much.

Siderite said...

You are very very welcome. :)

petergun said...

I've pulled the rest of my hair out, before I found your post.
It was very helpful. Thanks a lot!

Anonymous said...

Beware xslt, you need to do something like this - substitute angle brackets for parentheses:
(xsl:text disable-output-escaping="yes")(![CDATA[(script type="application/javascript" src="myfile.js")(/script)]])(/xsl:text)

Siderite said...

What the hell does xslt have to do with anything?

Anonymous said...

Wow, this was causing us a huge headache loading javascript in a webview in our mobile app. Leaving off the end tag worked on Android Gingerbread and iOS 4, but died quietly on Android Honeycomb and iOS 5. Thanks so much!

Ati said...

i should have found your blog a week ago.. many many thanks for this piece of information!

Anonymous said...

Anonymous said...
Beware xslt, you need to do something like this - substitute angle brackets for parentheses:
(xsl:text disable-output-escaping="yes")(![CDATA[(script type="application/javascript" src="myfile.js")(/script)]])(/xsl:text)


Thanks this saved my life..i was going bonkers

Siderite ...see xslt has to do with it as it causes on one script to load..so I ended up here and that post saved me hours of work..