View Full Version : Simple JavaScript Function which are frequently used in JavaScript

02-25-2008, 12:25 PM
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...

function getObj(id)
var obj = null;
//DOM (Netscape 6, Explorer 5)
obj = window.document.getElementById(id);
else if(document.all)
obj = window.document.all[id];
//Remove this final else if for Netscape 4 for more info visit
// this site (http://www.quirksmode.org/js/layerwrite.html)
else if(document.layers)
obj = window.document.layers[id];
return obj;
return alert('Object Not Exists or Your browser does not support this operation.');

Same code will be available in here (http://www.dreamportal.co.in/silverlining/blogs/)

02-25-2008, 03:03 PM
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.

02-25-2008, 03:05 PM
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.

02-26-2008, 02:44 AM
Thanks for your suggestions jscheuer1

I will update myself

02-28-2008, 11:21 AM
To get the browser's environment...:confused: 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 (http://www.w3schools.com/)

Same thing you can find in dreamportal (http://www.dreamportal.co.in/silverlining/blogs/?p=85)

02-28-2008, 01:26 PM
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.

02-28-2008, 04:34 PM
Thanks... But As a beginner in JavaScript i used these... So i thought for the beginners it will be useful...atleast some extend... :-(

02-28-2008, 05:29 PM
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.

02-28-2008, 09:13 PM
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 :)

02-28-2008, 09:48 PM

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

02-29-2008, 01:26 AM
I am by no definition an expert in the varied scripting languages available to web designers today, but I do know my way around code, and for all it's obvious failings there is one thing that I continue to use W3Schools for...a reference guide for tags. It may not alway be accurate and updated, but for my purposes using W3Scools or devguru.com's quick ref in collaboration with Quirksmode.org's compatibilities tables is a very helpful tool.;)

05-06-2008, 02:38 PM
Thanks for all your advises

05-08-2008, 05:25 AM

The page has a list of some JS function frequently needed. Most of these functions comes now with popular JS frameworks like jQuery, YUI, etc.

05-08-2008, 06:48 AM

Those are all perhaps good ideas, but there are several cases where they should be more complete, and/or where they assume things that might not be the case in a valid document. I got disgusted after a few of them and stopped reading.

Two examples:

function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
else {
window.onload = function() {

What if oldonload() required information on the load and possibly other event(s) depending upon how it came to be executed by other code. In browsers other than IE, and perhaps even in IE, that information would no longer be available to it.

function insertAfter(parent, node, referenceNode) {
parent.insertBefore(node, referenceNode.nextSibling);

What if there is no nextSibling? This one may just 'magically' work out in those cases, but I tend to doubt it.