PROGRAMMING SOLUTIONS

Monday, 3 August 2009

Get HTML source using javascript


var source = document.getElementsByTagName('html')[0].innerHTML


Test it!

Javascript prompt


var name = prompt("Whats your name?", "");
if (name != "") {
alert("Welcome " + name);
}



Test it!

Sunday, 2 August 2009

Handle cookies in javascript

Here's an object to manipulate cookies, you can set, get or delete a cookie.


function CookieHandler() {

this.setCookie = function (name, value, seconds) {

if (typeof(seconds) != 'undefined') {
var date = new Date();
date.setTime(date.getTime() + (seconds*1000));
var expires = "; expires=" + date.toGMTString();
}
else {
var expires = "";
}

document.cookie = name+"="+value+expires+"; path=/";
}

this.getCookie = function (name) {

name = name + "=";
var carray = document.cookie.split(';');

for(var i=0;i < carray.length;i++) {
var c = carray[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
}

return null;
}

this.deleteCookie = function (name) {
this.setCookie(name, "", -1);
}

}

Yes/No message box in javascript

There are many times where we want ask or confirm about an action through a page, this can be achieved using the "confirm" javascript function. The "confirm" function returns 1 if the "OK" button was pressed and 0 for the "Cancel" button.

Example:

function msgboxYesNo() {
if (confirm("Are you sure you want to move to the next page?")) {
window.location.href = "nextpage.html";
}
}


Test it!

How to validate an email address using javascript


function validEmail(email){
var emailReg = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
return emailReg.test(email);
}

Drag and drop in javascript

Copy-paste the following code at the top of your html file, inside the head tag.


var DragDropHandler = {


// private property.
_oElem : null,


// Attach drag handler to an element, public function
attach : function(oElem) {
oElem.onmousedown = DragDropHandler._dragBegin;

// callbacks
oElem.dragBegin = new Function();
oElem.drag = new Function();
oElem.dragEnd = new Function();

return oElem;
},


// Begin drag process , private function
_dragBegin : function(e) {
var oElem = DragDropHandler._oElem = this;

if (isNaN(parseInt(oElem.style.left))) { oElem.style.left = '0px'; }
if (isNaN(parseInt(oElem.style.top))) { oElem.style.top = '0px'; }

var x = parseInt(oElem.style.left);
var y = parseInt(oElem.style.top);

e = e ? e : window.event;
oElem.mouseX = e.clientX;
oElem.mouseY = e.clientY;

oElem.dragBegin(oElem, x, y);

document.onmousemove = DragDropHandler._drag;
document.onmouseup = DragDropHandler._dragEnd;
return false;
},


// Drag (move) element , private function
_drag : function(e) {
var oElem = DragDropHandler._oElem;

var x = parseInt(oElem.style.left);
var y = parseInt(oElem.style.top);

e = e ? e : window.event;
oElem.style.left = x + (e.clientX - oElem.mouseX) + 'px';
oElem.style.top = y + (e.clientY - oElem.mouseY) + 'px';

oElem.mouseX = e.clientX;
oElem.mouseY = e.clientY;

oElem.drag(oElem, x, y);

return false;
},


// Stop drag process , private function
_dragEnd : function() {
var oElem = DragDropHandler._oElem;

var x = parseInt(oElem.style.left);
var y = parseInt(oElem.style.top);

oElem.dragEnd(oElem, x, y);

document.onmousemove = null;
document.onmouseup = null;
DragDropHandler._oElem = null;
}

}

Saturday, 1 August 2009

How to open a popup window using javascript

Pass the following parameters to the OpenPopup fucntion and call it through your javascript code.


url2open = "http://developers-hell.blogspot.com/";
w = 400;
h = 300;
wndName = "Developers-hell";

function OpenPopup(url2open, w, h, wndName) {
mywindow = window.open (url2open, wndName, 'location=1,status=1,scrollbars=1,width='+w+',height='+h+'');
mywindow.moveTo(20,20);
mywindow.focus();
}


Test it!