Category Archives: Java Script

All abou Java script

Visualization of Few Data Structures and Algo’s using JavaScript.

Hello Readers,

I am currently working on a small fun type project to setup visualization of Few Data Structures and Algo’s using JavaScript. The project is hosted on Github, Feel free to fork and suggest. Thanks stay blessed!!

Link can be found here.

Common DOM methods in Java Script

Below is the lists of few common DOM methods that can be used on most elements in a document. As DOM gives us generic access to the element, their attributes, styles defined in a given document.

1. getElementById(object_id) : This method is used to access a object with its ID, The only thing we should keep in mind that before calling this method the target object should be created on the DOM  tree, Otherwise error will be thrown.

syntax: <div style=”background-color:black”></div>

2 appendChild(node): Inserts the specified node or object at the end of the current container object. A frequently used method for dynamically appending a new element or text to the document.

3 insertBefore(newElement, targetElement): This inserts a node before the target node to the same Parent Node.

syntax : document.getElementById(“myId”).insertBefore(newCild, andTheOldTargetChild)

4 insertAfter(newElement, targetElement): This inserts a node after the target node to the same Parent Node.

syntax: document.getElementById(“myId”).insertBefore(newCild, andTheOldTargetChild)

5 removeChild(childreference) : This method is used to remove a child from a Given Parent Node.

syntax : document.getElementById(“myId”).removeChild(childId);

6 getAttribute(attributeName): Returns the value of the attribute named attribute of the current node.


7 getElementsByClassName: This method is very useful and widely used some of the known JS lib frameworks. This method return the reference of all the objects who has the same CSS class name assigned. It will return an array collection.

var arr[]=document.getElementById(“myId”).getElementsByClassName(“subMenus”)

8 getElementsByTagName(tagName): This method is having same nature of the above with only a difference that it sends the list of collection by TAG name.

var tags[]=document.getElementsByTagName(“a”) //returns all anchor tags list on page

9 hasAttribute(attributename): This method is used to verify the if a particular attribute has been defined for a given Node.

if (document.getElementById(“myId”).hasAttribute(“style”)){

//returns true;


10 setAttribute(attributename, value, [iecaseflag]): As the name said we can use this method to set attribute on a given node. and before assigning use the “hasAttributes” method.

Syntax: document.getElementById(“myId”).setAttribute(“valign”)=”center”

11 removeAttribute(attributename): As a name suggest this is used to remove a attribute.

syntax: document.getElementById(“myId”).removeAttribute(“style”);

12 innerHTML Property : The easiest way to get or modify the content of an element is by using the innerHTML property. innerHTML is not a part of the W3C DOM specification. However, it is supported by all major browsers. The innerHTML property is useful for returning or replacing the content of HTML elements (including <html> and <body>), it can also be used to view the source of a page that has been dynamically modified.

Hope This helps.

R Vashi

Using eval() method in Java Script

The eval() method accepts a string of JavaScript statements(method, expressions)  and evaluates it as JavaScript source code.
eval() returns the value of the executed JavaScript statement.

WE mainly use eval() method while working with AJAX response which sometime contains some executable java script code to run.
Even many of the Java script lib/framework uses this heavily in their AJAX frameworks.

1. eval(statement) //statement can be any java script method.

2. eval(string) //any raw string containing the java script expressions. e.g eval(“alert(‘hello’)”);
First, eval() determines if the argument is a valid string, then eval() parses the string looking for JavaScript code. If it finds any JavaScript code, it will be executed the same way it execute the other methods defined in script tag.

[NOTE] This method is widely supported by most of the known browsers like IE, Mozilla, OPera, Safari, Chrome.

Hope this helps.

R Vashi

Calling Browser Function for Fullscreen window using Java Script on IE

Hi All,

In this article I will show how to call browser function to full screen the window using Java Script+ActiveX. This example is based on IE. although it is very easy to use method in Java script to achieve the same but sometime we might face the need to use the same behavior which IE browser F11 gives.

function fullScreenWindow()
// Get the ActiveX object from the Window
var acvxObj= new ActiveXObject("");
// Now sends a key to fullscreen


You can use the same function for any toggling full screen effect.

[NOTE] Whenever you call this method you will get a confirmation from IE Browser for ActiveX use.

Hope this helps.

R Vashi.

Detecting Browsers using Java Script

Hi All,

Although it is not a rocket science to get the browser details using any technology script, but i will show a very Object oriented way to display the browser details using java script.

Step 1: Create one JS file, Name it CheckBrowser.js

Step 2: Write the below code in the file.

function IsBrowser(){
var agt=navigator.userAgent.toLowerCase();
this.major = parseInt(navigator.appVersion);
this.minor = parseFloat(navigator.appVersion);
this.nav  = ((agt.indexOf(‘mozilla’)!=-1) && ((agt.indexOf(‘spoofer’)==-1)  && (agt.indexOf(‘compatible’) == -1))); // FireFox/Mozilla

// add your browsers detection code here.
// e.g


Step 3: Add the js in the Html/JSP/ASP/PHP file.
// <![CDATA[
javascript” >
// ]]>

Step 4: Invoke the function in Object oriented way

var is = new IsBrowser();

R Vashi

Closing browser window or tab in Mozilla/Firefox

Hi All,

Sometime we face the problem in closing a window/tab in Mozilla Firefox. The reason is that Firefox configuration doesn’t allows users to close active tabs or windows which are not opened by the script. The script means here refers to the pop up windows, opened using “” method.

Below I will share a very tricky steps to do that.

–          Open Mozilla
–          Type “about:config” in address bar
–          You will see a list of some configurations
–          Search for “dom.allow_scripts_to_close_windows” and toggle to “true”, by default value will be “false”

Now create one HTML page, and add the below java script method to close the window.


function closeWin(){


R Vashi

Reading query parameters using Java Script


One of the best mechanism passing data between pages is query strings in the URL. As we know server side scripts like JSP,SERVLETS,ASP,PHP supports very easy way to parse query paremeters,

But  Java script being a client side script also supports a very easy way to parse the query parameters in the URL.

Use the below method and see the how easy we achived.


var url = window.location.toString(); // read the URL
var prms = RegExp.$1;

var prms = prms.split(“&”); //SPLIT THE VALUES IN ARRAY
var stringList = {};

for(var i=0;i<prms.length;i++){
var tmpKey = prms[i].split(“=”);
stringList[tmpKey[0]] = unescape(tmpKey[1]);

for(var j in stringList){
document.write(j+” = “+stringList[j]+”<br/>”);


R Vashi