C
A
T
E
G
O
R
I
E
S

&

R
E
C
E
N
T

Javascript - Definitely written in the 90s

Posted by tigerhawkvok on March 15, 2009 17:56 in computers , programming

Higgs is still coming, but I wanted to comment on the absurdity of Javascript. It lacks an elementary function — the ability to import code from an existing external file (within an existing javascript script). Even the obvious hack doesn't work:

<script type='text/javascript'>
document.write("<script type='text/javascript' src='code.js'></script>");
</script>

So, there, I'm trying to import "code.js" to execute within a greater block of Javascript. The practical upshot of this is that you can, for example, conditionally run a block of code (say, with this only executing when an IF statement evaluates). So, what's wrong with this little code block? I mean, its a bit hack-y, but really, what's wrong? Well that code outputs:

");

The answer? You can't have a script closing tag in the document.write function, and to the best of my knowledge, you can't self-close a script tag. Thus, you need to hack it like so:

<script type='text/javascript'>
document.write("<script type='text/javascript' src='code.js'></scr"+"ipt>");
</script>

Small change, but amazingly nonobvious. Took me a long time to figure it out. And thus ends this rant.

Moral of the story? Stick with PHP and CSS. They're more standards-compliant, and way more intuitive. For example, they'd just use:

<?php
include("code.php"); // code.inc in some contexts ?>

or

<style type='text/css'>
@import "css_sheet.css"; /* This has different uses from <link> */
</style>

Much better, and much more intuitive!

Lets close this with something a bit less "ranty". It turns out that, finally, you can customize your logon background in Windows 7 without third party hackery — though you still need to do a few reg changes to pull it off.

Information and Links

Join the fray by commenting, tracking what others have to say, or linking to it from your blog.



Trackbacks

InspiredPosters.com

InspiredPosters.com | 16/12/2014 14:38

Javascript - Definitely written in the 90s | The Dichotomous Trekkie 2.0