Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Ajax returning values 1

Status
Not open for further replies.

snowboardr

Programmer
Joined
Feb 22, 2002
Messages
1,401
Location
PH
I got a code snippet here from BabyJeff, and it works great for ajax in all the browsers i needed it to work in... but I am having problems returning innerHtml with this exact code... now I have code that does return the html correctly, but it doesnt work in IE.. could someone help me mesh these two codes together?


This is the code i use now, but can't get it working with the innerHtml (this code works for saving however)
Code:
var xmlHttp

function SaveCollapse(intCall,SaveThis,queryid)
{ 
var url="ajax_save.asp?call=" + intCall + "&save=" + SaveThis + "&id=" + queryid
xmlHttp=createRequestObject()
xmlHttp.open("GET", url , true)
xmlHttp.send(null)
} 


function createRequestObject() {
    var ro;
    /*@cc_on
    @if (@_jscript_version >= 5)
        try {
            ro = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                ro = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (E) {
                ro = false;
            }
        }
    @else
        ro = false;
    @end @*/
    if (!ro && typeof XMLHttpRequest != 'undefined') {
        try {
            ro = new XMLHttpRequest();
        } catch (e) {
            ro = false;
        }
    }
    return ro;
}

This code works in returning innerHTML, I have tried everything in regards to try and "join" the two together etc.. Because i would like as many browsers as i can to work.

Regards,
Jason

Code:
var xmlHttp

function showCustomer(str)
{ 
var url="ajax_save.asp?call=4&sid=" + Math.random() + "&save=" + str
xmlHttp=GetXmlHttpObject(stateChanged)
xmlHttp.open("GET", url , true)
xmlHttp.send(null)
} 

function stateChanged() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{ 
document.getElementById("searchresults").innerHTML=xmlHttp.responseText 
} 
} 

function GetXmlHttpObject(handler)
{ 
var objXmlHttp=null

if (navigator.userAgent.indexOf("Opera")>=0)
{
alert("This example doesn't work in Opera") 
return; 
}
if (navigator.userAgent.indexOf("MSIE")>=0)
{ 
var strName="Msxml2.XMLHTTP"
if (navigator.appVersion.indexOf("MSIE 5.5")>=0)
{
strName="Microsoft.XMLHTTP"
} 
try
{ 
objXmlHttp=new ActiveXObject(strName)
objXmlHttp.onreadystatechange=handler 
return objXmlHttp
} 
catch(e)
{ 
alert("Error. Scripting for ActiveX might be disabled") 
return 
} 
} 
if (navigator.userAgent.indexOf("Mozilla")>=0)
{
objXmlHttp=new XMLHttpRequest()
objXmlHttp.onload=handler
objXmlHttp.onerror=handler 
return objXmlHttp
}
}
 
The following example ought to work for you. It's designed to target output to a container on the page with an id="content". I can't test it right now, but it ought to work fine.
Code:
function createRequestObject() {
    var ro;
    /*@cc_on
    @if (@_jscript_version >= 5)
        try {
            ro = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                ro = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (E) {
                ro = false;
            }
        }
    @else
        ro = false;
    @end @*/
    if (!ro && typeof XMLHttpRequest != 'undefined') {
        try {
            ro = new XMLHttpRequest();
        } catch (e) {
            ro = false;
        }
    }
    return ro;
}

var SaveCollapseHttp = null;
function SaveCollapse(intCall,SaveThis,queryid)
	SaveCollapseHttp = createRequestObject();
	var url="ajax_save.asp?call=" + intCall + "&save=" + SaveThis + "&id=" + queryid
	SaveCollapseHttp.open('get', url, true);
	SaveCollapseHttp.onreadystatechange = getSaveCollapseResponse;
	SaveCollapseHttp.send('');
}

function getSaveCollapseResponse() {
	if (SaveCollapseHttp != null)
		if (SaveCollapseHttp.readyState == 4)
			if (SaveCollapseHttp.status == 200)
				document.getElementById('content').innerHTML = SaveCollapseHttp.responseText;
}

...
<div id="content"></div>
Cheers,
Jeff

[tt]Jeff's Page @ Code Couch
[/tt]

What is Javascript? FAQ216-6094
 
Thanks, for your help jeff, however its not returning any results. I can see the error in IE, it says "Object expected line 19, however line number in ie for js errors is never accuracte so that wont help much...

jason

the only thing i changed in your code was document.getElementById('searchresults').innerHTML = SaveCollapseHttp.responseText;

Code:
function createRequestObject() {
    var ro;
    /*@cc_on
    @if (@_jscript_version >= 5)
        try {
            ro = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                ro = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (E) {
                ro = false;
            }
        }
    @else
        ro = false;
    @end @*/
    if (!ro && typeof XMLHttpRequest != 'undefined') {
        try {
            ro = new XMLHttpRequest();
        } catch (e) {
            ro = false;
        }
    }
    return ro;
}

var SaveCollapseHttp = null;
function SaveCollapse(intCall,SaveThis,queryid)
    SaveCollapseHttp = createRequestObject();
    var url="ajax_save.asp?call=" + intCall + "&save=" + SaveThis + "&id=" + queryid
    SaveCollapseHttp.open('GET', url, true);
    SaveCollapseHttp.onreadystatechange = getSaveCollapseResponse;
    SaveCollapseHttp.send('');
}

function getSaveCollapseResponse() {
    if (SaveCollapseHttp != null)
        if (SaveCollapseHttp.readyState == 4)
            if (SaveCollapseHttp.status == 200)
                document.getElementById('searchresults').innerHTML = SaveCollapseHttp.responseText;
}
 
Ah, that was my fault - I forgot to include the opening bracket in the function SaveCollapse():
Code:
function SaveCollapse(intCall,SaveThis,queryid) [!]{[/!]
Sorry about that,
Jeff

[tt]Jeff's Page @ Code Couch
[/tt]

What is Javascript? FAQ216-6094
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top