...

View Full Version : How would you combine these scripts in one?



alstefani
11-29-2008, 05:15 PM
Hi Guys!
I'm really bad with JavaScript (better in ASP). Can you tell me how to combine these 2 scripts in one? the only difference between the 2 is the ajax url call:

Here is the javascript:


function ajaxFunction1(){
var ajaxRequest1; // The variable that makes Ajax possible!

try{
// Opera 8.0+, Firefox, Safari
ajaxRequest1 = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest1 = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest1 = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
// Create a function that will receive data sent from the server
ajaxRequest1.onreadystatechange = function(){
if(ajaxRequest1.readyState == 4){
var ajaxDisplay1 = document.getElementById('ajaxDiv');
ajaxDisplay1.innerHTML = ajaxRequest1.responseText;
}
}


ajaxRequest1.open("GET", document.getElementById('First').getAttribute("href"), true);
ajaxRequest1.send(null);
}

function ajaxFunction2(){
var ajaxRequest2; // The variable that makes Ajax possible!

try{
// Opera 8.0+, Firefox, Safari
ajaxRequest2 = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest2 = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest2 = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
// Create a function that will receive data sent from the server
ajaxRequest2.onreadystatechange = function(){
if(ajaxRequest2.readyState == 4){
var ajaxDisplay2 = document.getElementById('ajaxDiv');
ajaxDisplay2.innerHTML = ajaxRequest2.responseText;
}
}

ajaxRequest2.open("GET", document.getElementById('Previous').getAttribute("href"), true);
ajaxRequest2.send(null);
}

And here are the HTML trickers:

<table border="0" width="100%">
<tr>
<td><% If MM_offset <> 0 Then %><a href="<%=MM_moveFirst%>" id="First" onclick="ajaxFunction1();return false">&lt;&lt;First</a> <% End If ' end MM_offset <> 0 %></td>
<td><% If MM_offset <> 0 Then %><a href="<%=MM_movePrev%>" id="Previous" onclick="ajaxFunction2();return false">&lt;Previous</a> <% End If ' end MM_offset <> 0 %></td>
</tr>
</table>
Please help!

194673
11-29-2008, 09:32 PM
Option 1


function getAJAX()
{
var xmlHttp; // The variable that makes Ajax possible!

try{
// Opera 8.0+, Firefox, Safari
xmlHttp = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
return xmlHttp;
}

function ajaxFunction1(){
var ajaxRequest1 = getAJAX();
// Create a function that will receive data sent from the server
ajaxRequest1.onreadystatechange = function(){
if(ajaxRequest1.readyState == 4){
var ajaxDisplay1 = document.getElementById('ajaxDiv');
ajaxDisplay1.innerHTML = ajaxRequest1.responseText;
}
}


ajaxRequest1.open("GET", document.getElementById('First').getAttribute("href"), true);
ajaxRequest1.send(null);
}

function ajaxFunction2(){
var ajaxRequest2 = getAJAX();
// Create a function that will receive data sent from the server
ajaxRequest2.onreadystatechange = function(){
if(ajaxRequest2.readyState == 4){
var ajaxDisplay2 = document.getElementById('ajaxDiv');
ajaxDisplay2.innerHTML = ajaxRequest2.responseText;
}
}

ajaxRequest2.open("GET", document.getElementById('Previous').getAttribute("href"), true);
ajaxRequest2.send(null);
}

Option 2:
JS:


function ajaxFunction(elem){
var ajaxRequest1; // The variable that makes Ajax possible!

try{
// Opera 8.0+, Firefox, Safari
ajaxRequest1 = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest1 = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest1 = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
if
// Create a function that will receive data sent from the server
ajaxRequest1.onreadystatechange = function(){
if(ajaxRequest1.readyState == 4){
var ajaxDisplay1 = document.getElementById('ajaxDiv');
ajaxDisplay1.innerHTML = ajaxRequest1.responseText;
}
}

var action;
if (elem == "First")
action = document.getElementById('First').getAttribute("href");
else if (elem == "Previous")
action = document.getElementById('Previous').getAttribute("href");
ajaxRequest1.open("GET", action), true);
ajaxRequest1.send(null);
}

HTML:

<table border="0" width="100%">
<tr>
<td><% If MM_offset <> 0 Then %><a href="<%=MM_moveFirst%>" id="First" onclick="ajaxFunction(this.id);return false">&lt;&lt;First</a> <% End If ' end MM_offset <> 0 %></td>
<td><% If MM_offset <> 0 Then %><a href="<%=MM_movePrev%>" id="Previous" onclick="ajaxFunction(this.id);return false">&lt;Previous</a> <% End If ' end MM_offset <> 0 %></td>
</tr>
</table>

Edit: not tested by the way, so may have some errors.

alstefani
11-30-2008, 12:01 AM
Option 2: worked like charm. Thanks 194673

194673
11-30-2008, 03:50 AM
glad it worked :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum