Archive

Archive for the ‘Javascript’ Category

Check object exists or not

April 12, 2011 Leave a comment

We can check through javascript whether html element exists or not.
create this function
function checkobject(obj) {
if (document.getElementById(obj)) {
return true;
}else {
return false;
}
}

Pass element’s id in this function, this function return ‘true’ if that element exists on web page other wise return false.

 

Advertisements

How to get which element clicked

April 11, 2011 Leave a comment

We can find out which element clicked on web document.
First we need to create a function i.e
function gotElement(){
var ele = arguments[0] || window.event, origEl = ev.target || ev.srcElement;
alert(ele.tagName);//Getting  tag name of element.
}

Now call this function like this…
document.onclick = gotElement();

Whenever user click on click on document then a alert will open that contains tag name.

How to check browser has javascript enabled or not

April 7, 2011 Leave a comment

The <noscript> tag is used to provide an alternate content for users that have disabled scripts in their browser or have a browser that doesn’t support client-side scripting.

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.1//EN” “http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”&gt;

<html xmlns=”http://www.w3.org/1999/xhtml&#8221; xml:lang=”en”>
<head>
<title>Title</title>
<noscript>
<meta http-equiv=”refresh” content=”0;url=http://www.domain.com/errorPage.php”/>
</noscript>
</head>
<body>
</body>
</html>

Add and Remove element

October 28, 2010 1 comment

We can add any element through javascript. It is so simple and easy.  Use below script to add and remove a div element.

<html>
<head>
<title>Add Element</title>
<script language=”javascript”>
this.num = 1;
function addElement(){
$top = document.getElementById(‘top’);
newId = document.createElement(‘div’);
id = ‘my’+this.num;
newId.setAttribute(‘id’, id );
newId.innerHTML = “<a href=’javascript:void(0); ‘ onclick=’removedThis( “+id +”)’ >Added Element”+id+”</a>”;
$top.appendChild(newId);
this.num++;
}
function removedThis( id ){
var d = document.getElementById(‘top’);
d.removeChild(id);
}
</script>
</head>
<body>
<input type=”button” name=”button” value=”Add Element” onclick=”addElement()” />
<div id=”top” ></div>
</body>
</html>

Print content of a element

November 25, 2009 Leave a comment

If you want to print content of a html element, with the help of javascript you can do this.  You need to call  this function and pass a argument ( element id ). Take a look of this function.

<script type=”text/javascript”>
function printSelection(node){
var content=node.innerHTML
var pwin=window.open(”,’print_content’,’width=12800,height=1000, scrollbars=1′);
pwin.document.open();
pwin.document.write(‘<html><body onload=”window.print()”>’+content+'</body></html>’);
pwin.document.close();
//setTimeout(function(){pwin.close();},1000);
}
</script>

Now time to call this function. I called this function in a hyperlink like this.
<a href=”” onclick=”printSelection(document.getElementById(‘foo’));return false”>Print</a>

When you click on “Print” then print dialog appears give print command and enjoy this script.

Click here

Categories: Javascript

DOM

November 9, 2009 Leave a comment
1.) document.getElementById(‘id’): You can access virtually any element in            the DOM. Here is the syntax …
document.getElementById(‘id’)
Example:
a.) var obj    =    document.getElementById(‘id’);
alert(obj.value); {get value of a element}
alert(obj.title);  {get title of a element}
obj.style.color=’red’;  {Change color of a element}
b.) var obj    =    document.getElementById(‘id’).value;{get value of a element}
var obj1  =    document.getElementById(‘id).title; {get title of a element}

 

The different node methods available through DOM manipulation are as follows:
node.childNodes
node.firstChild
node.lastChild
node.parentNode
node.nextSibling
node.previousSibling
Categories: Javascript

Object Oriented JavaScript

November 4, 2009 Leave a comment

Creating a class

Use object oriented javascript.
Below code write in .js file.
First of all declare a class. In JavaScript  class declaration same as function.
Let create a class in JavaScript
function MyClass(){
}
Now create a function in this class
function MyClass(){
this.firstFunction = firstFunction;
function firstFunction(){
alert(“I am from class”);
}
Now call the class function through object.  So we need to create object that
class.
var obj = new MyClass();
obj.firstFunction();

OutPut: A alert msg “I am from class”.

%d bloggers like this: