Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
Sign in to follow this  
insertwackynamehere

Javascript

Recommended Posts

Okay first of all, before I begin, I used to be a Javascript hater, thinking that it ruined the web as we know it. But then along came Ajax and I decided to put that hate aside because Ajax is actually a pretty cool technology. So please, don't post here bitching about Javascript.

Anyway, here we go, basically for people who do anything with Javascript, could you please explain why IE doesn't appear to support it properly? I've been coding something in Firefox which worked fine in IE. I then proceeded to change some stuff, and suddenly it doesnt work in IE! It happened after I fixed every error Firefox spit out at me. What am I doing wrong? I don't know where to begin, I can't post examples because I dont know wtf. This 100% error free Javascript that works perfectly in Firefox and not at all (anymore) in IE. Yes, Javascript is enabled in IE. Is there anything off the top of anyone's heads that I could be doing wrong?

EDIT:
On closer look apparently my function to set up XMLHttpRequest object (the backbone of Ajax) that is supported by Firefox and IE is wrong somehow. It worked before but not now that i made it a function. Heres the code:

function createRequest() {
     var request = false;
     try {
          alert("firefox");
          request = new XMLHttpRequest();
     } 
     catch (microsoft) {
          try {
               alert("ms1");
               request = new ActiveXObject("Msxml2.XMLHTTP");
          }
          catch (oldmicrosoft) {
               try {
                    alert("ms2");
                    request = new ActiveXObject("Microsoft.XMLHTTP");
               }
               catch (failed) {
                    alert("fail");
                    request = false;
               }
          }
     }
     if (request == false) {
          alert("Error initializing XMLHttpRequest! Please use an up-to-date browser that supports Javascript!");
     }
     else {
          return request;
     }
}
function getContent(url) {
     alert("getContent "+url);
     request = createRequest();
     request.open("GET", url, true);
     request.onreadystatechange = changePage;
     request.send(null);
}

function changePage() {
     alert("changePage1");
     if (request.readyState == 4) {
          alert("changePage2");
          if (request.status == 200) {
               alert("changePage3");
               var response = request.responseText;
               document.getElementById("content").innerHTML = response;
               document.getElementById("message").innerHTML = "";
          }
          else {
               alert("Status is " + request.status + ". Data failed to load.");
          }
     }
}
The properties and methods of all the objects are the same, but for some reason IE doesnt recognize them when I call the function and create a new object from it. All the alerts("") are there for debugging. Could someone help? My IE is using ActiveXObject("Msxml2.XMLHTTP") as it's XMLHttpRequest object.


EDIT: Okayy... finally I figured it out. My "if (request == false)" statement in my createRequest function was throwing it off. But why?? I solved the problem temporarily by commenting out the check to see if the request exists.

Share this post


Link to post

This may sound rudementary but have you tried using a slightly different syntax, like if (!request) or something similar?

Share this post


Link to post

In to say exactly what goober said. I don't remember how Javascript deals with Null and False, so it's worth checking it out.

You can always alert(..) yourself with the offending vars or statements to see what the hell they're up to.

Share this post


Link to post

firstly, don't use try/catch as it's a relatively new addition to javascript and straight out breaks in browsers that do not support it

secondly, you should always allow your script to degrade, it looks here like you're changing the contents of a div depending on the reqest, something that can easily be achieved with an old fashioned clunky full page refresh such

<a href="mypage.asp?paneldata=12" onclick="changePanelData(12);return false;">foo</a>
check out my own attempt at this sort of ajax thingamajig here

http://www.cyclomedia.co.uk/?27

specifically the source is here

http://www.cyclomedia.co.uk/script/ajax_object.js


one thing i find that helps is a debug script, make a textarea on your page called, say "debug" (i even absolutely position it bottom right) and use it like a console:
	function debug( s )
	{
		if( document.forms["f"] && document.forms["f"]["d"] )
			document.forms["f"]["d"].value = document.forms["f"]["d"].value + s;
	}
because it appends the text you can log the execution of your script, and select all the text and delete it manually to do a clean test

edit: this may help too, IE7 now supports XMLHTTPRequest natively http://blogs.msdn.com/ie/archive/2006/01/23/516393.aspx

Share this post


Link to post

thanks for all the replies, unfortuantly mroe problems have arrisen, i'll go into detail later, but basically ff, ie and opera all seem to render differently, and ff is the only one thats "correct" (at least based on what I want). There seems to be a lot to figure out. Thanks for the replies :) Also, IE7 will support XMLHttpRequest directly but for backwards compatibably people still will have to use this clunky "if ie then" code, which is yet another reason how ms screwed over web developers. bleh :P thanks for the help tho :)

Share this post


Link to post

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  
×