JavaScript


15
Feb 08

Convert DOM to String

While working on one of my projects I came across a scenario where I needed to convert the DOM object into a String representation to send it as an AJAX request parameter.

The way to do this is as follows:

Suppose you have a form named “Test”.

We first get the DOM object of the current page as follows:

var domObject = document.Test;

//Now lets convert this DOM to String

var domToString = domObject.innerHTML;

Thats it!!! Put an alert and you’ll see the complete DOM in a string representation.

alert(domToString);


12
Feb 08

Confirm Box (‘OK’,'CANCEL’ button Message Box)

Here’s how you display a confirm box using JavaScript.

JavaScript has a function named ‘confirm()‘ which displays the confirm box.

var confirmResult = confirm(“Clicking OK will return true whereas clicking Cancel will return false”);

When you call a JavaScript function containing the above code, a confirm box with two options namely ‘OK’ and ‘CANCEL’ will be displayed. We make use of a variable ‘confirmResult’ to hold the value returned by the ‘confirm()’ function when the user presses ‘OK’ or ‘Cancel’.

A simple example below will make everything clear:

function testingConfirmBox()

{

var confirmBoxReturnValue = confirm(“Click any of the two buttons below and I’ll display an alert telling you the name of the button you clicked :) ”);

if(confirmBoxReturnValue == true)

{

alert(“You clicked ‘OK’ “);

}

else

{

alert(“You clicked ‘Cancel’ “);

}

}

From the poll results of this post I found that this post did not really help a few people. If you did not get what you expected from this post then please leave a comment with your query. May be I would be able to help :)

Note: As far as I know, there is no way of changing the ‘OK’ ‘Cancel’ text to something like ‘Yes’ and ‘No’ unless you hack the browsers source code.


1
Feb 08

Display text in the browser status bar

To display text in the status bar of your browser using javascript write the following code inside your javascript function:

window.status = “This will display text in the status bar of your browser!”;

Note: The status bar may not change in firefox. In such a case go to “tools-options” click on “content” tab, click on the “advanced” for “enable javascript” and enable the “change status bar text” option.