Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Nov 2008
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    How would you combine these scripts in one?

    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!

  • #2
    Regular Coder
    Join Date
    Dec 2006
    Posts
    166
    Thanks
    9
    Thanked 4 Times in 4 Posts
    Option 1
    Code:
    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:
    Code:
    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:
    Code:
    <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.

  • Users who have thanked 194673 for this post:

    alstefani (11-30-2008)

  • #3
    New to the CF scene
    Join Date
    Nov 2008
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Thumbs up

    Option 2: worked like charm. Thanks 194673
    Last edited by alstefani; 11-30-2008 at 01:01 AM. Reason: edit

  • #4
    Regular Coder
    Join Date
    Dec 2006
    Posts
    166
    Thanks
    9
    Thanked 4 Times in 4 Posts
    glad it worked


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •