.comment-link {margin-left:.6em;} <$BlogRSDURL$>

Thursday, December 15, 2005

Javascript Weirdness 

This a continuation of the Safari Weirdness post below. You'll no doubt recall that in Safari if you create a button from Javascript you get an ugly square thing that doesn't look anything like the cool buttons you get when you hard-code the button in the HTML. Well it gets even stranger after that. According to the AJAX book I've been using, the best way to attach a callback action to a button is like this:
var theButton = document.createElement("button");
theButton.appendChild(document.createTextNode("Click Here You Big Silly"));
theButton.onclick = function () { respondToTheClick() };
If you do that you get your button, assuming you remembered to add it to the DOM somewhere. But when you click on it. It just doesn't work. No matter what you do, the xmlHttp.status you get back when the server routine prints its output is always always always undefined. This makes no sense to me. If you look at the Javascript console in Firefox the error message starts with "Component returned failure code 0x80040111" with some other gobbledy gook after that. Safari just reports xmlHttp.status as undefined. Now here comes the weird part. If instead of creating your button on the fly, you hard code it into the HTML
<input type = "button" value = "Click Here You Silly" onClick = "respondToTheClick();">
(and since you obviously only want it available sometimes otherwise you wouldn't be wanting to create it on the fly in the first place) and put it in a div element that is set to display:none which you toggle to display:block when you want the User to see the button, the button works like a charm and the returned xmlHttp.status is 200 just like you want it to be. Weeeeee!

That was a large part of yesterday morning. Googling the Firefox error message fragment I quoted just gets you a bunch of people saying "hmm... it's a bug in Firefox. Maybe they'll fix it soon."

So there you go AJAX people, use hardcoded buttons and hide them until you need them.


Comments: Post a Comment


This page is powered by Blogger. Isn't yours?