Forum Moderators: open

Message Too Old, No Replies

Help with javascript getcontent

         

JuicyScript

5:00 pm on Mar 27, 2010 (gmt 0)

10+ Year Member



I want to load a page into this page..So that all pages will b loaded here#### to reduce load time on the server###
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript" src="jquery.js"></script>
<script>
function getContent(page, params, elementid, waitmsg){
//params has to have following format
//i.e.: c=1&id=3....
//page is the server side script. include full path
//i.e. ../scripts/myscript.php


//Clear our fetching variable
var xmlhttp=false;
//Try to create active x object
try {
xmlhttp = new ActiveXObject('Msxml2.XMLHTTP');
} catch (e) {
try {
xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
} catch (E) {
xmlhttp = false;
}
}
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
xmlhttp = new XMLHttpRequest();
}

//This is the path to the PHP file on the server
var file = page+params;

//Open the file through GET, and add the page we want to retrieve as a GET variable
xmlhttp.open('GET', file, true);

xmlhttp.onreadystatechange=function() {
if(xmlhttp.readyState == 1){

//Display waiting image and message while content loads.
//Make sure you have the corect path for the image
document.getElementById(elementid).innerHTML = "<p><img src='images/waiting.gif' align='middle'> Please wait... <br />"+waitmsg+ "</p>" ;

//Check if it is ready to recieve data
}else if(xmlhttp.readyState==4) {
//Make sure there is something in the content variable
var content = xmlhttp.responseText;

//The content data which has been retrieved
if( content ){
//Change the inner content of your div to the newly retrieved content
document.getElementById(elementid).innerHTML = content;
}
}
}
//Nullify the XMLHttpRequest
xmlhttp.send(null)
return;
}
</script>
</head>

<body>
EXAMPLE TEXT:WHAT IS YOUR NAME
<a href="javascript:getContent('../page.php', '?li=info', 'info','')">Info</a>


</body>
</html>

Fotiman

12:54 am on Mar 28, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The first thing I notice is that your AJAX code is not really correct. It's better to use the native XMLHttpRequest object vs. using an ActiveXObject. See:
Cross Browser XMLHttpRequest Explained [webmasterworld.com].

Also, there is no such thing as a "javascript:" protocol, therefore your href value is incorrect as well. Instead, put your JavaScript code in the onclick handler. For example:

<a href="../page.php" onclick="getContent(this.href, '?li=info', 'info','');return false;">

Otherwise, I don't see a question here. Are you having a problem with this code?

JuicyScript

1:21 am on Mar 28, 2010 (gmt 0)

10+ Year Member



Am new to the AJAX fin can u please correct the script for me because when i click on the info link nothing happens

daveVk

4:42 am on Mar 28, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I note you are using jquery

<script type="text/javascript" src="jquery.js"></script>

Read the documentation on there website on using ajax, that should make it much easier

[api.jquery.com...]

eg

<a href="javascript:getContent('../page.php', '?li=info', 'info','')">Info</a>

becomes something like

<a href="#" onclick="$('#info').load('../page.php/?li=info');return false">Info</a>

and rest of the javascript can go