Script working in Firefox but not IE7

T

TheDude5B

Hi, I have this piece of AJAX Javascript which I copied from the
w3schools.com website, which is supposed to work with most browser,
yet it doesnt work with IE7.

<script type="text/javascript">
var xmlHttp

function countCartItems()
{
//if (str.length==0)
//{
//document.getElementById("cartcountfield").innerHTML="";
//return;
//}
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
alert ("Your browser does not support AJAX!");
return;
}
var url="getCartCount.aspx";
//url=url+"?q="+str;
//url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}

function stateChanged()
{
if (xmlHttp.readyState==4)
{

document.getElementById("cartcountfield").innerHTML=xmlHttp.responseText;
}
}

function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
</script>


Can someone help me out here to why this is not working with IE7?

Thanks
 
E

Erwin Moller

TheDude5B said:
Hi, I have this piece of AJAX Javascript which I copied from the
w3schools.com website, which is supposed to work with most browser,
yet it doesnt work with IE7.

Hi,

A quick pointer: the GetXmlHttpObject() DOES work in IE7.
At least on my Vista Home Premium IE7.

This looks allright too:
document.getElementById("cartcountfield").innerHTML=xmlHttp.responseText;

Could you elaborate WHAT is not working in your situation?

Does your counter not get invoked?
Do you get some error?

Maybe add an alert to stateChanged() function to see if it gets called
at all?

Did you try to debug by placing a few strategically placed alerts?

Regards,
Erwin Moller
 
T

TheDude5B

Hi,

A quick pointer: the GetXmlHttpObject() DOES work in IE7.
At least on my Vista Home Premium IE7.

This looks allright too:
document.getElementById("cartcountfield").innerHTML=xmlHttp.responseText;

Could you elaborate WHAT is not working in your situation?

Does your counter not get invoked?
Do you get some error?

Maybe add an alert to stateChanged() function to see if it gets called
at all?

Did you try to debug by placing a few strategically placed alerts?

Regards,
Erwin Moller










- Show quoted text -

When running in IE7, I have but alerts in which appear at each
important stage. An alert fires when it trys to set the xmlHttp=new
XMLHttpRequest(); (for Firefox and other browsers), but does not then
continue to the catch statement which is where it should go. It then
does not fire the alert after this code:
if (xmlHttp==null)
{
alert ("Your browser does not support AJAX!");
return;
}

Also, while debugging my application (it is an asp.net app) the code
never runs into the var url="getCartCount.aspx" page when in IE7, but
does in Firefox.
This is very strange as I remember using this same bit of code a while
back and it all seemed to work.

Is there another way which I could do the same thing?
 
T

TheDude5B

It seems that this code does work with standard HTML and asp, but does
not work on IE7 with .aspx files?
 
E

Erwin Moller

TheDude5B said:
It seems that this code does work with standard HTML and asp, but does
not work on IE7 with .aspx files?

Hi Dude,

I don't know the first thing about .NET, so I cannot help there.

But when debugging javascript, there is 1 simple rule: simply look at
the source the server delivers to the browser.
That is all the browser cares about, NOT how/by what it is generated.

A tip: since you use FF already, get the following add-on in:
HTML Validator (version 0.8.4.0 is the latest I think)

It works of course only in FF, but this tool DID save many hours of
mindless debugging because it tells you when you view source WHAT is
illegal code.
Great place to start. Even though you do not have problems in FF.

I found stupid things that are hard to find, like PHP-code coming from a
MAC that used bad END-OF-LINE, that didn't show up in my editor.

Chances are you'll quickly find strange stuff in your code if you use
this add-on.

Give it a shot. Good luck.

Regards,
Erwin Moller
 
P

Paul E. Schoen

"Erwin Moller"
Hi Dude,

I don't know the first thing about .NET, so I cannot help there.

But when debugging javascript, there is 1 simple rule: simply look at the
source the server delivers to the browser.
That is all the browser cares about, NOT how/by what it is generated.

A tip: since you use FF already, get the following add-on in:
HTML Validator (version 0.8.4.0 is the latest I think)

It works of course only in FF, but this tool DID save many hours of
mindless debugging because it tells you when you view source WHAT is
illegal code.
Great place to start. Even though you do not have problems in FF.

I found stupid things that are hard to find, like PHP-code coming from a
MAC that used bad END-OF-LINE, that didn't show up in my editor.

Chances are you'll quickly find strange stuff in your code if you use
this add-on.

Give it a shot. Good luck.

I would also highly recommend Firebug (www.getfirebug.com), where you can
set breakpoints and use mouseover with the cursor to examine values of
variables. It was suggested to me in my newbie post on the microsoft
Jscript NG.

Paul
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top