Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: Simple JavaScript Function which are frequently used in JavaScript

  1. #1
    Join Date
    Jul 2007
    Location
    India
    Posts
    7
    Thanks
    1
    Thanked 1 Time in 1 Post

    Lightbulb Simple JavaScript Function which are frequently used in JavaScript

    We have been using javascript for a two years of period.
    We came across lot of places where we were frequently used lot of javascript methods.
    so We start from here the most frequently used javascript method...
    Code:
    function getObj(id)
    	{
    		if(window.document)
    		{
    			var obj = null;			
    			//DOM (Netscape 6, Explorer 5)
    			if(document.getElementById)
    				obj = window.document.getElementById(id);
    			//IE4 DOM
    			else if(document.all)
    				obj = window.document.all[id];
    			//Remove this final else if for Netscape 4 for more info visit 
    			// this site		
    			else if(document.layers)
    				obj = window.document.layers[id];			
    			if(obj){
    				return obj;
    			}
    			else{
    				return alert('Object Not Exists or Your browser does not support this operation.');
    			}
    		}
    	}
    Same code will be available in here

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    As tempting as it is to have a sort of 'universal getter' like that, there is little point in including early Netscape (layers), as once you have the element, doing anything with it will almost always also involve a separate code path from the other two. The decision to support document.all is a bit sketchy as well because you need to go back to IE 4 to get that without getElementById also being available, and once again the entire code path, though not quite as often as with layers, may well need to be different.

    BTW, testing for layers is usually carried out first, as often the browsers that use it will otherwise barf on tests for the other two methods.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    The ones I use most are functions such as map(), reduce(), zip(), &c. It's quite amazing how many situations can be made simpler with the application of a couple of these.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. #4
    Join Date
    Jul 2007
    Location
    India
    Posts
    7
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Thanks for your suggestions

    Thanks for your suggestions jscheuer1

    I will update myself

  5. #5
    Join Date
    Jul 2007
    Location
    India
    Posts
    7
    Thanks
    1
    Thanked 1 Time in 1 Post

    Lightbulb To get the browser's environment

    To get the browser's environment... use them well

    var navigator = window.navigator;
    var browser = navigator.appName;
    var b_version = navigator.appVersion;
    var version = parseFloat(b_version);
    var appcodename = navigator.appCodeName;
    var platform = navigator.platform;
    var cookie = navigator.cookieEnabled;
    var userAgent = navigator.userAgent;
    var appMinorVersion = navigator.appMinorVersion;
    var cpuClass = navigator.cpuClass;
    var onLine = navigator.onLine;
    var browserLanguage = navigator.browserLanguage;
    var systemLanguage = navigator.systemLanguage;
    var userLanguage = navigator.userLanguage;


    use them well

    Thanks to W3 Schools

    Same thing you can find in dreamportal
    Last edited by YouKnowWho; 02-28-2008 at 11:27 AM.

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Better, don't use them at all -- browser detection is bad.

    A lot of those are IE-only. http://fn-js.info/jsbot?q=w3schools.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  7. The Following User Says Thank You to Twey For This Useful Post:

    YouKnowWho (02-28-2008)

  8. #7
    Join Date
    Jul 2007
    Location
    India
    Posts
    7
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default

    Thanks... But As a beginner in JavaScript i used these... So i thought for the beginners it will be useful...atleast some extend... :-(

  9. #8
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by Twey View Post
    Better, don't use them at all -- browser detection is bad.

    A lot of those are IE-only. http://fn-js.info/jsbot?q=w3schools.
    W3schools is better than that link makes out. But they aren't perfect. Generally when they use IE centric stuff, it still works out in other browsers, at least as far as the other browsers can (degrades well). As for any specific code being crap, I'm sure it worked when originally posted. Probably still works, but may possibly not be to standards.

    It is a very well organized and put together tutorial site. Others may have more accurate/current information, but few if any, are so well organized and novice friendly.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  10. #9
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    However, whatever value the site may have to novices is made more or less worthless by the fact that a novice will be unable to tell good information from bad.

    If you check W3Schools' forums, countless people have tried to post corrections to the content, and all have been completely ignored by whoever maintains it.
    It is a very well organized and put together tutorial site.
    That's not true -- it repeats itself often, it offers no clear path to follow for the novice, and the content is split into separate, overly-small pages and broken by advertisements such that reading is a pain. Even if it were beautifully laid out, the fact that most of it is just plain wrong means that it does more harm than help. Better for the novice to struggle through a rather technical explanation over the course of a few weeks before going out to produce well-designed and -thought-out websites than to start designing production sites on the first day and never realise how bad their code is. I suspect this approach to teaching is an important factor in why the Web is in the state it is today.
    Generally when they use IE centric stuff, it still works out in other browsers, at least as far as the other browsers can (degrades well)
    If it did, it wouldn't be IE-centric
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  11. #10
    Join Date
    Jan 2007
    Posts
    51
    Thanks
    2
    Thanked 3 Times in 3 Posts

    Default

    YouKnowWho....

    If you are looking for a good javascript tutorial that will let you know about browser issues...

    JavaScript - A Definitive Guide - O'Reilly

    Or just Google many sites on your topic then compare and contrast what you find. Put together small single purpose web files to test your idea, see what works and what does not on different browsers.

    Also, John pointed out something above about supporting really old browsers. This might not be needed, scan your web logs and see what browsers are accessing your site.
    I just chose the top 5, and said forget the rest.

    As for W3 Schools... It's decent on a few things for non-commercial development.... but do your homework if you are getting paid to write code. It's helped me get my brain around CSS.

    - my 2 eurocents

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •