Showing posts with label javascript. Show all posts
Showing posts with label javascript. Show all posts

Ajax request

Simple reusable asynchronous multibrowser ajax function.

Call a page by javascript and pass the html result to a function when it arrives (. The url to load by HTTP GET method, and the callback function name are passed as parameters. All the litteral content of the result page is passed as parameter.

Note: some security features, like in Firefox, can block AJAX request to other domains than the domain of the calling webpage (where the script is executed from).


function ajaxRequest(url, callbackFunction)
{
var xmlhttp=false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
// JScript gives us Conditional compilation, we can cope with old IE versions.
// and security blocked creation of the objects.
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}
@end @*/
if (!xmlhttp && typeof XMLHttpRequest!='undefined')
{
xmlhttp = new XMLHttpRequest();
}
xmlhttp.open("GET", url,true);
if(callbackFunction != null)
{
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState == 4)
{
callbackFunction(xmlhttp.responseText);
}
}

}
xmlhttp.send(null);

}
function sampleCallBackFunction(httpResponseText)
{
alert(httpResponseText);
}
// sample calls
//
ajaxRequest("http://www.someurl.com",sampleCallBackFunction);
ajaxRequest(document.location,sampleCallBackFunction);//show current page source

Changing cursor (mouse pointer) in javascript

// for a hourglass cursor
window.document.body.style.cursor = "wait";

// for a normal cursor
window.document.body.style.cursor = "default";

Adding global validation before submit (javascript)

Context: need for validation or special javascript behaviour before POST.

Since the asp.net postback mecanism occurs with html POST method, it is possible to add an javascript event that replace the normal post event to force a special behaviour or validation.

This function define a pointer to the normal post event of the form, than it define a behaviour that will occur when the post event will fire. This behaviour decides if the form will really submit (ie firing a asp.net postback event).


// setting the handler of the main form:
SetSubmitHandler(document.forms[0]);

// handler definition to personalize
function SetSubmitHandler(form)
{
// setting a pointer to the original submit function
// to really post the form to the server
form.orginalSubmit = form.submit;

// function that replaces the normal post function
form.newSubmit = function()
{
// special behaviour
// .....
if (1==1)// validation goes here..
{
// validation is ok --> post to the server
form.orginalSubmit();
}
}
// replacing the standard handler (posting to the server)
// with the new handler
form.submit = form.newSubmit;
}

Changing url between frames in a frameset

Need a javascript than can, from a frame, relaod another frame with a new url -- or a url with new parameters?

// by frame id (in the parent frameset)
parent.frames["OtherFrameId"].window.location = "http://...."

// or by index
parent.frames[2].window.location = "http://..."

Setting and retrieving cookies in javascript

Here are two function to use cookies directly from javascript. The firt one stock a pair of key/value in the cookie collection. The second one retrieve the value for a key.


function setCookie(idCookie,value)
{
var cookie_date = new Date (); // current date & time
var milli = cookie_date.getMilliseconds()+5*60*1000; // == cokie valid for 5 minutes
cookie_date.setMilliseconds(milli);
cookie_date.setTime ( cookie_date.getTime());
document.cookie = idCookie += "=" + value +"; expires=" + cookie_date.toGMTString();
}

function getCookie ( idCookie )
{

var results = document.cookie.match ( idCookie + '=(.*?)(;|$)' );

if ( results )
return ( unescape ( results[1] ) );
else
return null;
}