Archive

Archive for the ‘Javascript’ Category

How to view source of a show modal dialog page

March 17th, 2009 admin No comments

We can view a page source normally from the menu view->source in mozilla or ie.But how would we view a page source of a showmodal page.

we can achieve it by the following code.

function viewSource() 
{
d=window.open();
d.document.open('text/plain').write(document.documentElement.outerHTML);
return false;
}

Popularity: 3% [?]

How to read XML using Javascript?

February 23rd, 2009 admin No comments

The Document Object Model (DOM) as implemented in MSXML provides a programmatic representation of XML documents, fragments, nodes, or node-sets. It also provides an application programming interface for working with XML data.

The following JScript fragments outline the basic process of programming with XML DOM.

var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");

Example:

function loadXML(xmlFilePath)
{
  try //Internet Explorer
  {
    xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  }
  catch(e)
  {
    try //Firefox, Mozilla, Opera, etc.
    {
      xmlDoc=document.implementation.createDocument("","",null);
    }
    catch(e)
    {
      alert(e.message)
    }
  }
 
  try
  {
    xmlDoc.async=false;
    xmlDoc.load(xmlFilePath); // xmlFilePath - xml file path Ex: test.xml
    return(xmlDoc);
  }
  catch(e)
  {
    alert(e.message)
  }
  return(null);
}

Popularity: 1% [?]

Categories: Javascript Tags: , ,

setInterval Method in Javascript

January 19th, 2009 admin No comments

The setInterval() method evaluates a function or an expression repeatedly for the specified number of milliseconds and it returns an interval reference.

Syntax:

variable = setInterval(expression, milliseconds)

or

variable = setInterval(function, milliseconds)

variable holds the interval reference returned by seInterval method.

Example:
Below example display the current time information using setInterval method call the date object.

<html>
<head>
<title> setInterval Method Example </title>
<script language="javascript">
  function startSetInt()
  {
    ref=setInterval("init()",100);
  } 
 
  function init()
  {
    dt = new Date();
    document.getElementById("lbl").innerText = "Timer : " + dt;
    document.getElementById("btnStart").disabled = true;
    document.getElementById("btnStop").disabled  = false;
  }
 
  function stopSetInt()
  {
    clearInterval(ref)
    document.getElementById("lbl").innerText = "Timer : ";
    document.getElementById("btnStart").disabled = false;
    document.getElementById("btnStop").disabled = true;
  }
</script>
</head>
 
<body onload="startSetInt()">
<label id="lbl" name="lbl"></label>
</br>
</br>
 
<input type="button" id="btnStart" value="Start" onclick="startSetInt()" />
<input type="button" id="btnStop" value="Stop" onclick="stopSetInt()" />
 
</body>
</html>

Demo : Click here

Popularity: 2% [?]

Categories: Javascript Tags: ,

JSON Data types and Example

January 12th, 2009 admin No comments

The following are the Basic JSON Data types,

  • Number : integer, real, or floating point
  • String : double-quoted Unicode with backslash escaping
  • Boolean : true and false
  • Array : an ordered sequence of values, comma-separated and enclosed in square brackets
  • Object : collection of key:value pairs, comma-separated and enclosed in curly braces
  • null

Example

{
"firstName": "Praveen",
"lastName": "C",
"address": {
"streetAddress": "KK Nagar",
"city": "Chennai",
"state": "TN",
"postalCode": 6310102
},
"phoneNumbers": [
"091 98943 37266"
]
}

Popularity: 1% [?]

Categories: Javascript Tags: ,

What is JSON?

January 12th, 2009 admin No comments

JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition – December 1999. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others.

Popularity: 1% [?]

Categories: Javascript, What-is Tags: ,

Split Function in Javascript

December 1st, 2008 admin No comments

Split Function: Delimiter :-
The space character ” ” we mentioned will be our delimiter and it is used by the split function as a way of breaking up the string. Every time it sees the delimiter we specified, it will create a new element in an array. The first argument of the split function is the delimiter.

Split() Function:-
The split() function splits text and puts the pieces into an array, with each piece taking up one slot in the array. For example, if you were to split “This is some text”,the split() function would create an array with four slots (zero to 3). The first slot in the array would contain “This”, the second slot would contain “is”, the third “some” and the fourth “text”.

Example:-

 

var stringdata= "1$2$3$4";
var splitdata = stringdata.split("$");
for(i = 0; i < splitdata.length; i++)
{
   document.write("Element " + i + " = " + splitdata [i]);
}


Output
=====
Element 0 = 1
Element 1 = 2
Element 2 = 3
Element 3 = 4

Popularity: 1% [?]

Categories: Javascript Tags: ,

Date Function in Javascript

December 1st, 2008 admin No comments

1.create a date object and store it in a variable

var d= new Date();

 

2.Using the date object,Get the hour,minute,second,month,dayofmonth and year of the Current Date

var hour        = d.getHours();
var minute      = d.getMinutes();
var second      = d.getSeconds();
var month = d.getMonth();
var dayofmonth    = d.getDate();
var year        = d.getYear();

Example:-
Display Next Day of Today

function getNextDate()
{
var date= new Date();
var day = date.getDate();
var month = date.getMonth();
var year = date.getYear();
 
var nextdate= new Date(year, month, day +1);
var nxtDate=nextdate.getMonth()+1+"/"+nextdate.getDate()+"/"+NextDate.getYear();
document.getElementById('DisplaynextDate').value=nxtDate;
//Display Next Date(today+1)
}

Popularity: 1% [?]

Categories: Javascript Tags: ,

Disable Right Mouse Click Using JavaScript

November 19th, 2008 admin 1 comment
Description:
This script will prevent the default right menu from popping up when the right mouse is clicked on the web page. Works well in IE4+ and NS4+.

To use this script, paste the following code in the <body> section of your HTML document.
 

Disable right click on mouse

 

Function to handle right click if the browser is IE

<script language="JavaScript">
var message=”Right Click is Disabled”;
function ExeForIE()
{
  if (event.button==2)
  {
     alert(message);
     return false;
  }
}
</script>

 

Function to handle right click if the browser is FireFox

<script language=JavaScript>
var message=”Right Click is Disabled”;
function ExeForFFox(evt)
{
  if (document.layersdocument.getElementById&&!document.all )
  {
    if (evt.which==2evt.which==3)
    {
      alert(message);
      return false;
    }
  }
}
 
if (document.layers)
{
  document.captureEvents(Event.MOUSEDOWN);
  document.onmousedown = ExeForFFox;
}
else if (document.all&amp;&amp;!document.getElementById)
{
  document.onmousedown = ExeForIE;
}
document.oncontextmenu=new Function(alert(message);return false)
</script>

Popularity: 1% [?]

Block JavaScript Errors

November 18th, 2008 admin No comments

We can have a function with return true and use it on window.onerror.Thus when the browser encounters a JavaScript error it returns true and block the error begin displayed.

function blkError()
{
  return true;
}
window.onerror = blkError;

Popularity: 2% [?]

Categories: Javascript Tags: ,

Add a Row Dynamically using JavaScript

November 9th, 2008 admin No comments

How to Add a Row Dynamically Using JavaScript:

  • We can see how button,textbox,textarea can be added Dynamically
  • All the tag which can be given in the input tag has to be given in setAttribute as shown in below coding
  • if u call a function then call as but1.onclick = function () {show(this)};
  • If u delete the Row,then reorder all the other row

1.If u Add a Button to a Table,then use this

var cell1 = row.insertCell(0);
but1 = document.createElement('input');
but1.setAttribute('type', 'button');
but1.setAttribute('className', 'button');
but1.setAttribute('value', '');
but1.onclick = function () {show(this)};
cell1.appendChild(but1);

2.If u Add a Textbox to a Table,then use this

 
var cell2 = row.insertCell(1);
var txt = document.createElement('input');
txt.setAttribute('type', 'text');
txt.setAttribute('name', 'firsttextbox');
txt.setAttribute('className', 'text_box');
txt.setAttribute('id', 'firsttextbox' + iteration);
txt.setAttribute('size', '21');
cell2.appendChild(txt);

3.If u Add a Textarea to a Table,then use this

cell1 = row.insertCell(1);
var e1 = document.createElement('textarea');
e1.name = 'text2' + iteration;
e1.id = 'text2' + iteration;
e1.column= '50';
cell1.appendChild(e1);

4.Row can be Re-Order by using

var tbl = delRow.parentNode.parentNode;
tbl.tBodies[0].rows[i].myRow.two.id = 'firsttextbox' + count;

If u Delete 3 Row the Reorder as
4th Row as—-3th Row
5th Row as—-4th Row
6th Row as—-5th Row

5.If u want to Delete the Third Row,then use this coding

 
var delRow = obj.parentNode.parentNode;
var rowArray = new Array(delRow);
for (var i=0; i&lt;rowObjArray.length; i++)</div>
{
  var rIndex = rowObjArray[i].sectionRowIndex;
  rowObjArray[i].parentNode.deleteRow(rIndex);
}
  • We have to pass all the id in the row as deleterow(this)
  • this can be get in the obj variable
  • Using the For Loop we have to Delete the Particular Row

Demo Link

Popularity: 1% [?]

Categories: Javascript Tags: ,