[Javascript] detect js

Ryan Cannon ryan at ryancannon.com
Mon Jan 31 08:25:44 CST 2005


>
>
>Anyway, what I mean is: I don't really see anything better with this:
>
><script type="text/javascript">
>function writeStuff()
>{
>        document.write('<input type="text" ... />');
>} // end function
></script>
>
><div><script type="text/javascript">writeStuff();</script></div>
>
>than with this:
>
><p><script type="text/javascript">document.write(codename);</script></p>
>
>  
>

I think the idea here is to use neither method, and opt for:

<script type="text/javascript" src="somewhere/else.js"></script>
<noscript><p>The log-in form requires Javascript to functions</p></noscript>

This makes the script completely unobtrusive, as there is no actual 
script being interspersed among your HTML. The problem with 
document.write() then is that it's context specific--it will write where 
it's being called--making it difficult to use remotely. Instead, I can 
use DOM functions to do all the work form outside, using 
docuement.getElementById(), createElement(), setAttribute() and 
appendChild().

You can also test to make sure that the user's browser will support the 
javascript methods they need to access the form. While document.write() 
will just write, if you require more complex methods you can say

if (document.getElementById && document.getElementsByTagName && etc... ) {
// write the form here
}

While either method will work the DOM method is superior in  that it's
* Allows only browsers that can support your methods
* Less overhead for noscript browsers to download
* More readable and maintainable
* Will work in HTML, XHTML and XML
* More elegant

Ryan Cannon
http://www.ryancannon.com



More information about the Javascript mailing list