Javascript with maximum number of browsers

For the discussion of Perl, Python, Ruby, and PHP and other interpreted languages.

Javascript with maximum number of browsers

Post by fureto on Thu Sep 23, 2010 10:41 am
([msg=46360]see Javascript with maximum number of browsers[/msg])

Hello--as I've said in my posts in other threads, I'm very n00b, so please forgive the elementary question. Maybe this even belongs in the Nzone.

I'm doing my first reading about Javascript, and the [url="http://www.w3schools.com/js/js_howto.asp"]W3 Schools guide[/url] basically says it's good practice for browsers that can't read Javascript to enclose it in HTML comment tags.

Should one do this all the time, or only if your "browser audience" includes text-only programs like Lynx? I don't know of any other types of browsers that can't read Javascript. Maybe some smartphone browsers?

Thanks!
fureto
New User
New User
 
Posts: 21
Joined: Tue Sep 21, 2010 2:40 pm
Blog: View Blog (0)


Re: Javascript with maximum number of browsers

Post by Avery17 on Thu Sep 23, 2010 11:34 am
([msg=46362]see Re: Javascript with maximum number of browsers[/msg])

You should just do it anyways. It keeps your site looking clean if someone has javascript disabled or is routing through a proxy that disables scripts. Most mobile phones have javascript so really unless your audience is living under a rock with windows 95 and never update anything then your probably fine. It is always good practice to do such things though.
Avery17
Experienced User
Experienced User
 
Posts: 81
Joined: Fri Sep 17, 2010 11:28 pm
Blog: View Blog (0)


Re: Javascript with maximum number of browsers

Post by fureto on Thu Sep 23, 2010 1:48 pm
([msg=46365]see Re: Javascript with maximum number of browsers[/msg])

Thanks, those possibilities hadn't occurred to me. It seemed to me that it would be good practice, so when all of the examples following that one in the W3 Schools tutorial didn't have it I thought that was a little strange. I'd have thought they'd reinforce the good practice by example.
fureto
New User
New User
 
Posts: 21
Joined: Tue Sep 21, 2010 2:40 pm
Blog: View Blog (0)


Re: Javascript with maximum number of browsers

Post by tremor77 on Thu Sep 23, 2010 2:23 pm
([msg=46366]see Re: Javascript with maximum number of browsers[/msg])

Javascript should totally not be a problem but yes it would be good form to escape it. You can also add a 'noscript' tag. Here is a simple example.

Code: Select all
<script type="text/javascript">
   <!--
         document.write("Hello World!")
   -->
</script>
<noscript>Your browser does not support JavaScript!</noscript>


Another option is to put your javascript in another file, like myscript.js. This can clean up your page code very nicely, and works especially well if you use the same script on multiple pages.

Code: Select all
<script type="text/javascript" src="myscript.js">


You should begin to invest some time into learning PHP along with your Javascript and HTML. Purely for multi-page layout purposes there is nothing better than PHP's "include". As PHP is "server side" as opposed to Javascript's "client side" you can also replace some Javascript functions in which you would like to have more control over... Date & Time based scripts, randomizers and content generation being the most prominent. Javascript is still preferable to handle visual effects like rollovers and mouseovers.
Image
User avatar
tremor77
Contributor
Contributor
 
Posts: 860
Joined: Wed Mar 31, 2010 12:00 pm
Location: New York
Blog: View Blog (0)


Re: Javascript with maximum number of browsers

Post by fureto on Thu Sep 23, 2010 2:56 pm
([msg=46370]see Re: Javascript with maximum number of browsers[/msg])

Thanks tremor77. PHP is definitely on my list. You've given me some useful stuff to reread and look up definitions/explanations for again .... :)
fureto
New User
New User
 
Posts: 21
Joined: Tue Sep 21, 2010 2:40 pm
Blog: View Blog (0)


Re: Javascript with maximum number of browsers

Post by tgoe on Thu Sep 23, 2010 8:48 pm
([msg=46388]see Re: Javascript with maximum number of browsers[/msg])

User avatar
tgoe
Contributor
Contributor
 
Posts: 633
Joined: Sun Sep 28, 2008 2:33 pm
Location: q3dm7
Blog: View Blog (0)


Re: Javascript with maximum number of browsers

Post by fureto on Fri Sep 24, 2010 8:44 am
([msg=46409]see Re: Javascript with maximum number of browsers[/msg])

More good reading! Thanks.
fureto
New User
New User
 
Posts: 21
Joined: Tue Sep 21, 2010 2:40 pm
Blog: View Blog (0)


Re: Javascript with maximum number of browsers

Post by thetan on Fri Sep 24, 2010 10:50 am
([msg=46415]see Re: Javascript with maximum number of browsers[/msg])

The main argument to be made for wrapping javascript within html comment tags (or cdata tags) is that it's easier on the html parser to understand as it can simply disregard all data contained within the comment tags. So that way the finite state machine doing the lexical analyzation on the document can enter a much simpler state where all it's looking for is the end of the html comment other then children tags/etc. Most modern browsers should automatically do this however this is an efficiency hack for older browsers.

I highly suggest you look into some javascript libraries such as jQuery or mootools. They abstract all the cross browser issues from you so you can focus on the logic of the code rather then writing derivatives for each browser.
"If art interprets our dreams, the computer executes them in the guise of programs!" - SICP

Image

“If at first, the idea is not absurd, then there is no hope for it” - Albert Einstein
User avatar
thetan
Contributor
Contributor
 
Posts: 657
Joined: Thu Dec 17, 2009 6:58 pm
Location: Various Bay Area Cities, California
Blog: View Blog (0)


Re: Javascript with maximum number of browsers

Post by fureto on Fri Sep 24, 2010 2:48 pm
([msg=46430]see Re: Javascript with maximum number of browsers[/msg])

Thanks, thetan. I definitely will do that if I get into this seriously; right now, though, I'm just reading about Javascript to take the occasional break from my construction paralegal work, hoping I get through Basic 5. :P ;)
fureto
New User
New User
 
Posts: 21
Joined: Tue Sep 21, 2010 2:40 pm
Blog: View Blog (0)



Return to Interpreted Languages

Who is online

Users browsing this forum: No registered users and 0 guests