Solved: AJAX returns bad results in Internet Explorer

Note to self: IE6 likes to cache AJAX requests, and this can be a bad thing if other data on the page that will affect the result of the request has changed. You'll see this if you have two fields on a page that both contribute to a result, but only send them to the server when one of them has changed. Let's say field1 and field2 get multiplied on the server side, and both start out with values of 1. So, in IE6:

  • Set field1 to 1000: Response is 1000.
  • Set field1 to 100: Response is 100.
  • Set field2 to 123: Response is 12300
  • Set field 1 back to 1000: Uh oh! Response is 1000 again!
IE6 has decided that the result from the first time you set field1 to 1000 was good enough, and it doesn't look to see that the new result is different. The solution? Add a timestamp to your requests, so each one will look different to IE even if the real data is the same:
// just an example; you'd
// obviously build this from
// the field itself
var data = 'field1=1000'; 

// build the timestamp
var timestamp = new Date().getTime();
data = data + '&' + timestamp;

// now you can send your data 
// using ajax
Thankfully, when this happened to me yesterday, I had a coworker who only needed a couple of minutes with the server logs to figure out what was happening. After he figured it out here, I found this thread, which verified the cause, and I was able to solve it in a few minutes.

Loading mentions Retweet

Filed under  //  ajax   ie6   internet explorer   javascript  

Fixing width issues with IE6 print stylesheets

Note to self: remember to use !important when trying to deal with text running off the page in print versions of pages in IE6. Basic steps to follow:

  • EITHER have separate print and screen stylesheets ...
  • ... OR declare separate screen and print rule sets in a single stylesheet
    @media print {
      #foo {
        display:none;
      }
    }
    
    @media screen {
      #foo {
        display:block;
      }
    }
  • Put the content that you do want printed in containers that you can easily target with a CSS rule. Then, in the print css, display:none all the siblings of the containers that don't need to be printed. It may take a little trial and error to get the lists of printing and non-printing elements right, but well-designed, semantic HTML can make this task easier. Long story short, if you can display:none a parent element, you don't need to also display:none its children. The flipside of that is that if you do display:none a parent, you can't then display:[anything] children elements to get them to show up.
  • To deal with IE issues, give a fixed width to the containers you do want printed, and make the width !important:
    #content {
      width:400px !important;
      /* 400px seems to work well
      if you want to span the width
      of a letter-sized page */
    }
  • Also to deal with IE issues, you may find it useful to put some padding-right on your p elements.
  • If you have print rules that are required for IE but break things in other browsers, use a conditional comment to include the rules in a separate stylesheet.
Further reading: These are just some notes; interested in hearing other's tips!

Loading mentions Retweet

Filed under  //  css   ie6   print stylesheet