Thursday, December 23, 2010

after a request, page reloads 2 times and contains very little html

I just fixed a very strange issue in our application.

It went something like this:
User would click on a link that opened another page. This page would flash for a second, then another page would open containing just the string "2010".

The bug came from QA as being a regression related to recent development, so i started debugging in the code. There was nothing in the logs.

Then i opened firebug, went into "net" mode and persisted it. I saw my original request made, along with headers, and then another request made, this one had no headers at all.

Solution:

document.write is evil. There was recently a change to the code which attempted to make the copyright year dynamic by replacing it with
document.write(new Date().getFullYear());
inside of a script tag. This alone wouldn't cause the issue, but document.write only works correctly if its called when the page is being rendered. If the page is rendered, it will overwrite the entire page with whatever its writing out. The issue was the the page the user was going to next, was gotten via ajax, and thus the script was called after the page was loaded, overwriting the entire page.

Oh the glory of being a programmer.

No comments:

Post a Comment