SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Zealot
    Join Date
    Dec 2006
    Posts
    127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy problem using same ajax script more then once in a page.really need your help please.

    hi,i'm using a same ajax script more than once in my page.
    wonder,how can i make it to display the result in different <div>
    such as when user click on button 1,it will display info in div 1.
    when user click on button 2, it will display info in div 2.

    below is the script i using....

    ajax.js
    Code:
    function loadScript(scriptURL)
    {
    	var newScript = document.createElement("script");
    	newScript.src = scriptURL;
    	document.body.appendChild(newScript);
    }
    
    function loadData(URL)
    {
    // Create the XML request  
    	xmlReq = null;
    	if(window.XMLHttpRequest) 		xmlReq = new XMLHttpRequest();
    	else if(window.ActiveXObject) 	xmlReq = new ActiveXObject("Microsoft.XMLHTTP");
    	if(xmlReq==null) return; // Failed to create the request
    
    // Anonymous function to handle changed request states
    	xmlReq.onreadystatechange = function()
    	{
    		switch(xmlReq.readyState)
    		{
    		case 0:	// Uninitialized
    			break;
    		case 1: // Loading
    			break;
    		case 2: // Loaded
    			break;
    		case 3: // Interactive
    			break;
    		case 4:	// Done!
    		// Retrieve the data between the <quote> tags
    			doSomethingWithData(xmlReq.responseXML.getElementsByTagName('quote')[0].firstChild.data);
    			break;
    		default:
    			break;
    		}
    	}
    
    // Make the request
    	xmlReq.open ('GET', URL, true);
    	xmlReq.send (null);
    }
    in the index.php
    Code:
    <script language="JavaScript" src="ajax.js"></script>
    <script language="JavaScript">
    
    function doSomethingWithData(str)
    {
    	document.getElementById('div1').innerHTML = str;
    	document.getElementById('div2').innerHTML = str;//original not contain in the tutorial
    }
    </script>
    
    <div id="div1"></div>
    <div id="div2"></div>//original not contain in the tutorial
    <input type="button" value="view div 1" onclick="loadScript('stuffindiv1.js')">
    <input type="button" value="view div 2" onclick="loadScript('stuffindiv2.js')">

  2. #2
    SitePoint Zealot
    Join Date
    Dec 2006
    Posts
    127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    mmm.... i have discovered my method of doing this is wrong and will try on other method in ajax..

    please ignore this thread...

    thank you!


Bookmarks

Posting Permissions

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