Thursday, January 8, 2009

Stupid Firefox Tricks

I encountered what I think is a bug in the way Firefox handles Javascript. The fix to the problem was simply not to use any Javascript in the body of the page, and I'd be interested to see if anyone else has encountered this.

The Problem
I found what appears to be a thoroughly annoying bug on Firefox, and I'm wondering if anyone else has seen something similar. I suspect that it may have something to do with my configuration, otherwise I should have seen some other mention of it.

I was trying to embed a Feedburner blog summary in a new website, and this worked fine in IE. If you've never tried it, it involves sticking a chunk of Javascript in the middle of your page. Here is a sample (replace all instances of "[...]" with "<...>"):


[html][body]
...my other html...
[script src="http://feeds.feedburner.com/IndependentInDetroit?format=sigpro"]
...
[/body][/html]

The script tag returns:


document.write('[div class="feedburnerFeedBlock" id="IndependentInDetroit2842309"]');
document.write('[p class="feedTitle"]
[a href="http://independentindetroit.blogspot.com/"]Independent in Detroit[/a][/p]');
...(a dozen more lines of javascript-generated html)...
document.write('[/div]');

On the left is the IE screenshot. Notice that the site banner is at the top of the page, as expected. On the right is the Firefox screenshot, where you can see that the banner is not visible. It also says "Loading" which continues indefinitely or until you click the stop button.




Viewing the source shows something bizarre - the page contains only the expanded javascript. All of the other HTML (head, body, etc.) has been stripped off. It appears that Firefox interprets "script tag inside the body" to mean "redirect to a blank page containing only the javascript". Very odd.

The Solution
My fix was to do a urlfetch from Python (this site is hosted on the Google App Engine), then strip off the "document.write(' " and " ');" portions of each line, then stuff the HTML directly into the page. Here is a snippet of the (very simple) code. The second-last line is there because one of the steps (probably "theResult.content", which converts the result object to a String) escaped the single quotes in the string, so they came out looking like " \' ".

...
# Get the feedburner summary, strip out the javascript, then write it.
result =
urlfetch.fetch("http://feeds.feedburner.com/IndependentInDetroit?format=sigpro")
respOut.write(stripJavaScript(result))
...
def stripJavaScript(theResult):
outString = theResult.content.replace("document.write('", "")
outString = outString.replace("');", "")
outString = outString.replace("\\'", "'")
return outString

It worked well, but isn't a very satisfying solution. Has anyone else encountered this? Did I miss something obvious?