Script loads once but not twice?

Discussion in 'JavaScript' started by bigrollerdave, Feb 25, 2007.

  1. #1
    Okay I have a script that opens a box on the page and uses ajax to fill in the box with another page. If that makes sense at all? Anyways It works fine untill a users closes the box and then opens the box again. The box will show up both times but the ajax part wont load the second time the box is opened. I'm not sure why this is.

    This is the box javascript
    var originalDivHTML = "";
    var DivID = "";
    
    function buildDimmerDiv()
    {
        document.write('<div id="dimmer" class="dimmer" style="width:'+ window.screen.width + 'px; height:' + window.screen.height +'px"></div>');
    }
    
    function displayFloatingDiv(divId, title, width, height, left, top) 
    {
    	DivID = divId;
    
    	document.getElementById('dimmer').style.visibility = "visible";
        document.getElementById(divId).style.width = width + 'px';
        document.getElementById(divId).style.height = height + 'px';
        document.getElementById(divId).style.left = left + 'px';
        document.getElementById(divId).style.top = top + 'px';
    	document.getElementById(divId).className = 'dimming';
    	document.getElementById(divId).style.visibility = "visible";
    
    }
    
    
    function hiddenFloatingDiv(divId) 
    {
    	document.getElementById(divId).innerHTML = originalDivHTML;
    	document.getElementById(divId).style.visibility='hidden';
    	document.getElementById('dimmer').style.visibility = 'hidden';
    	
    	DivID = "";
    }
    
    
    buildDimmerDiv();
    
    Code (markup):
    This is the ajax to get the page and display it inside the box
    //Start Ajax Submit
        function createRequestObject() { 
    
           var req; 
        
           if(window.XMLHttpRequest){ 
              // Firefox, Safari, Opera... 
              req = new XMLHttpRequest(); 
           } else if(window.ActiveXObject) { 
              // Internet Explorer 5+ 
              req = new ActiveXObject("Microsoft.XMLHTTP"); 
           } else { 
              // There is an error creating the object, 
              // just as an old browser is being used. 
              alert('Problem creating the XMLHttpRequest object'); 
           } 
        
           return req; 
        
        } 
        
        // Make the XMLHttpRequest object 
        var http = createRequestObject(); 
    
    	//Query database to get username
        function sendRequest(words) { 
        
           // Open PHP script for requests 
           http.open('get', 'friend_send.php?username='+words); 
           http.onreadystatechange = handleResponse; 
           http.send(null); 
        
        }
        function submitRequest(words) { 
            var comment = document.form.comment.value;
           // Open PHP script for requests 
           http.open('get', 'friend_send.php?username='+words+'&send=1&comment='+comment); 
           http.onreadystatechange = handleResponse; 
           http.send(null); 
        
        }
    	
        function handleResponse() { 
        
           if(http.readyState == 4 && http.status == 200){ 
        
              // Text returned FROM the PHP script 
              var response = http.responseText; 
        
              if(response) { 
                 // UPDATE ajaxTest content 
                 document.getElementById("show_request").innerHTML = response; 
              } 
        
           } 
        
        } 
    
    Code (markup):
    And this is how I call the two functions
    <a href="javascript:displayWindow(), sendRequest('<?=$username?>')" style="color:#6690bb; font-size:11px;">Open</a>
    HTML:
     
    bigrollerdave, Feb 25, 2007 IP
  2. bigrollerdave

    bigrollerdave Well-Known Member

    Messages:
    2,112
    Likes Received:
    52
    Best Answers:
    0
    Trophy Points:
    140
    #2
    Do I have to http.close the page before I can open it again? I'm new to ajax so I'm not really sure.
     
    bigrollerdave, Feb 25, 2007 IP
  3. bigrollerdave

    bigrollerdave Well-Known Member

    Messages:
    2,112
    Likes Received:
    52
    Best Answers:
    0
    Trophy Points:
    140
    #3
    I made a demo of it here

    Click on the link then click on the close button then click on the link again.

    The ajax script works the first time and includes the ajax.php file but if you close the box and then open it again it wont include the ajax.php file.
     
    bigrollerdave, Feb 26, 2007 IP
  4. Aragorn

    Aragorn Peon

    Messages:
    1,491
    Likes Received:
    72
    Best Answers:
    1
    Trophy Points:
    0
    #4
    originalDivHTML = document.getElementById(divId).innerHTML;
    
    Code (markup):
    Insert the above code as line no. 11 of dimmingdiv.js and it will work fine. On changing the innerHTML of 'windowcontent', you failed to store the original content.
     
    Aragorn, Feb 27, 2007 IP
  5. bigrollerdave

    bigrollerdave Well-Known Member

    Messages:
    2,112
    Likes Received:
    52
    Best Answers:
    0
    Trophy Points:
    140
    #5
    Thanks works perfectly
     
    bigrollerdave, Feb 27, 2007 IP