Using Ajax, a short script
(Engelsk)
An ajax script that inserts a page into a div, or other element that supports the innerHTML attribute.
Very useful script! Just make sure to note that this uses GET and not POST so it's not the best option for submitting forms, however, it works amazingly well for updating information. For example if you have a list of products you need to sort by date, name, id or price - then you simple use this script to access a file that does the sorting for you.
Very useful script! Just make sure to note that this uses GET and not POST so it's not the best option for submitting forms, however, it works amazingly well for updating information. For example if you have a list of products you need to sort by date, name, id or price - then you simple use this script to access a file that does the sorting for you.
Javascript ->
/* AJAX FUNCTIONS */
function visIndhold(file,targetDiv,check_div,fader)
{
var HttpRequestVar = false;
var IsLoadingMessage = '';
var ErrorBesked = 'Your Browser Does Not Support Ajax!';
if(check_div)
{
var checkValue = document.getElementById(check_div).value;
}
else
{
var checkValue = '';
}
if(window.XMLHttpRequest) // FF Etc..
{
try
{
HttpRequestVar = new XMLHttpRequest();
}
catch(e)
{
HttpRequestVar = false;
}
}
else if(window.ActiveXObject) // IE
{
try
{
HttpRequestVar = new ActiveXObject("Msxml2.XMLHTTP"
;
}
catch(e)
{
try
{
HttpRequestVar = new ActiveXObject("Microsoft.XMLHTTP"
;
}
catch(e)
{
HttpRequestVar = false;
}
}
}
else
{
HttpRequestVar = false;
}
if(HttpRequestVar) // Is it supported?
{
var random = Math.random() * Date.parse(new Date()); // random string to prevent caching
var file_array = file.split('.'); // string or file?
if(file_array[1] == 'php') // call php file...
{
var queryString = '?rand=' + random;
}
else if(file_array[1] == 'htm' || file_array[1] == 'html') // call html file...
{
var queryString = '';
}
else // hopefully a php file with a queryString ^^
{
var queryString = checkValue + '&randstring=' + random;
}
HttpRequestVar.open("get", url_encode(file + queryString), true); // use get... open file contents
// handle the httprequest
HttpRequestVar.onreadystatechange = function ()
{
if(HttpRequestVar.readyState == 4) // done and responded
{
if(fader == "1"
{ } else {
document.getElementById('fader').style.display = 'none';
}
document.getElementById(targetDiv).innerHTML = HttpRequestVar.responseText; // put result into div
}
else
{
document.getElementById(targetDiv).innerHTML = IsLoadingMessage; // Still loading...
if(fader == "1"
{ } else {
document.getElementById('fader').style.display = 'block';
}
}
}
HttpRequestVar.send(null);
}
else
{
document.getElementById(targetDiv).innerHTML = ErrorBesked; // browser doesn't support ajax
}
}
// fixes the url string to make sure everything is up to par
.
function url_encode(string)
{
var string;
var safeChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/-_.&?=";
var hex = "0123456789ABCDEF";
var encodedString = "";
for(var i = 0; i < string.length; i++)
{
var character = string.charAt(i);
if(character == " "
{
encodedString += "+";
}
else if(safeChars.indexOf(character) != -1)
{
encodedString += character;
}
else
{
var hexchar = character.charCodeAt(0);
if(hexchar > 255)
{
encodedString += "+";
}
else
{
encodedString += "%";
encodedString += hex.charAt((hexchar >> 4) & 0xF);
encodedString += hex.charAt(hexchar & 0xF);
}
}
}
return encodedString;
}
