Rough Book

random musings of just another computer nerd

Month: June, 2005

innerHTML and createContextualFragment problems when Firefox renders XHTML

If any of you use Firefox, then you’ll have noticed that the “Preview Comment” feature doesn’t work anymore. It stopped working after I converted this site over to XHTML. Initially it worked (even though it validated as XHTML 1.1) because I was setting the mime-type to text/html. To make it completely compliant, I set the mime-type (for browsers that accept it) to application/xhtml+xml. This made certain things break in Firefox. Namely, innerHTML. HTMLElement.innerHTML isn’t a W3C standard anyway, so it would make sense that it wouldn’t work. However, it was not so much an error as it is a bug in Mozilla (or in Gecko).

I looked around for some alternatives and chanced upon the createContextualFragment function. It is supposed to work, but I guess those examples were for Firefox when it didn’t support the innerHTML property in HTML. I tried something like this:

var range = div_preview.ownerDocument.createRange();
            
range.selectNodeContents(div_preview);
range.deleteContents();

var fragment = range.createContextualFragment(comment); //<-- will crash here
div_preview.appendChild(fragment);

But that didn’t work either. I then found out that this is also a bug. I’ve got two pages demonstrating the bugs here and here.

I don’t have any other alternatives than to

  • Wait for Firefox 1.1
  • Parse the code I want to insert through an XML parser, and then create the objects through DOM Core methods

I figure I’ll try the option b for now, but I don’t know how the slow the parsing will be. I guess I won’t know until I try.

This site is XHTML 1.1 Valid

I finally got this site to validate! The folks over here were really helpful, and pointed out that I wasn’t assigning values to my objects’ style attributes properly. For example, I was doing:

obj.style.width = 200;

instead of:

obj.style.width = "200px";

The second way is the correct way because it is important to specify units for your attributes. In addition, Firefox won’t parse it because it’s very strict about XHTML and CSS. Furthermore, certain things weren’t where I expected them to be. I was used to writing:

obj.style.height = document.body.scrollHeight;

But that no longer worked (the value being returned was 0). The method that works now is:

obj.style.height = document.body.parentNode.scrollHeight + "px";

You have to add in that “px” because the scroll height doesn’t have any units. Once I made these changes, the site started working and validating perfectly. Thanks a bunch to all those who helped!

I guess I did most of this just to jump on the bandwagon and because it did have a “geek” factor. But eitherway, since my website adheres to the standards now, I can be reasonably sure that it looks like the way it should… except for IE though. IE lets you get away with almost anything, and that’s why 90% of the websites out there have such terrible code. IE is very forgiving and can recover from bad HTML, but that only encourages sloppy coding. But anway, now that my site validates, I can proudly display the W3C’s “Valid XHTML 1.1” icon!

I love this girl

Here’s an inanity:

I am totally in love with this girl (more pictures here). I was at Sadhana’s house one day, and her amma was watching some Tamil movie. The “song and dance” sequence was on, and I see this really cute girl on the screen. So I immediately turn to Kala Aunty and go “Who is that girl?!”. It turns out that her name is “Asin”. And she’s Malayalee too!+ Me, being the resourceful nerd that I am, immediately checks her out on Google (once I get home). I saw a few pictures, but not too many.

For some reason, I forgot about her for a few months and then suddenly one day I was like “I wonder how Asin is doing these days”. So I checked her out again. I got a lot of pictures this time. You know, this reminds me of the crush I had on Malaika Arora oh yeah, and I can’t forget my crush on Shakira… I guess boys never grow up.

Anyway, I had a bunch of time on my hands to waste so I went ahead and made a wallpaper. I usually have grand ideas about making wallpapers, but they never really come to fruition. I decided to make a very simple one this time. The end result is what you see below. You can click it to get the full-size version. It’s for a 1280×1024 resolution, but it should shrink without any problems to 1024×768.

Asin

+Yes, as if this greatly increases my chance of meeting her!

Live! Preview and XHTML Compatibility

I’ve added a new feature called “Live Preview”. You can see your comment as it will appear, right as you type it. I randomly just came up with the idea yesterday – I figured I could use the same code I have in place for the preview, for the Live Preview as well. I just trap the onkeyup event for the textarea. It works pretty well. Of course, there is a lag, but it’s not too bad. Oh yeah, I also removed the numbers in the comment list and replaced them with little blurbs that I made. The blurbs won’t show correctly in IE, because IE is stupid and doesn’t handle PNG transparencies well.

Then there’s XHTML compatibility. I’ve been trying really hard to get this page to validate. I was finally able to get rid of all the errors. This page validates (if you set the DOCTYPE to XHTML 1.1 explicitly, but I’ll get to that later) without any errors, as does all the CSS. However, the problem comes when I add in the DOCTYPE. The page validates perfectly, but displays completely messed up in Firefox (IE can’t handle the mime type so I didn’t even bother checking it with IE). I tracked the problem down to the Javascript. Basically, I can’t set any DOM object attribute values from Javascript. It’s as if all attributes are read-only. It’s really quite bizarre. I’ve posted the problem on a message board, so we’ll see what happens. If any of you know why I could be facing this problem, please let me know.

Here’s what the DOCTYPE (and other information) looks like:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">

Also, I have a sandboxed version of the website here. You can see the problem directly there.

Preview Button and no more Iframes

I have added a “Preview Comments” button to the comment submission form. So now, you’re able to preview your comments before submitting them. The preview will show you your comments exactly as they will appear (after they have been submitted).

The other big thing is the removal of iframes. I thought iframes were really freaking awesome when I first found out about them. And they are. There are a bunch of cool things you can do with them. I always used iframes to update the contents of a page without refreshing the entire page itself. This was accomplished by submitting to a hidden iframe. The server-side script in the iframe would generate Javascript, which would then manipulate the DOM on the parent page. Pretty nifty. I learnt that when I was working at College of Business at ASU. It was something that the bunch of us (Chris, Kelly, Gravey, Cameron, and I) came up with in the Fall of 2001. We thought it was pretty novel.

I used iframes extensively in the last incarnation of my website. When that crashed and burned, I needed to make a new website. So I went ahead and used iframes on this one too. Somewhere along the way, I was possessed by the urge to make this website XHTML compatible. But alas, iframes didn’t pass the compatibility test. I then tried to find a replacement for iframes. The object tag seemed to work, except there was no way to make it reload. Then I tried XMLHttpRequest. This also seemed promising, but I had to make a bunch of changes to the Javascript. I didn’t feel like doing that. I also then realized that I wanted content to show up on search engines, and having an iframe wasn’t going to do that. So I then decided to re-design the backend completely. Instead of using an iframe, I would just dump content directly into the page. Of course, now it wouldn’t load the content seamlessly when you clicked on a date, but what the heck. I still had one iframe though – the one that populates the calendar. I figured there simply wasn’t any other way to do it, so I left it as it is.

It stayed that way until Marc left me a comment talking about the need for the functionality to preview comments. I was going to use the iframe, before I realized that the comments could have line-breaks, and all other kinds of nasty stuff that could mess up Javascript. I could have accessed the HTML from the iframe, and dumped it into the page, but the first thing that came to my mind was the XMLHttpRequest object. I used it to replace the iframe. I think it actually works a whole lot better, and is definitely more flexible. Even though the object is used for XML, you can get HTML through it as well through the responseText attribute. This way, you can simulate the action of submitting to a form, and then access the data generated by the server-side script. You can then do whatever you want with the data. This is definitely much more flexible than having to deal with Javascript from the action iframe.

In the course of all this, I wrote some (I think) pretty nifty code. I wrote two functions; getXMLHttpRequestObject creates an XMLHttpRequest object and returns it. I needed this function because IE and Firefox create the object differently. The other function, sendXMLHttpRequest sends the request to the server. The cool part is that I also send in a custom handler. When sendXMLHttpRequest finishes receiving the data from the server, it calls the custom handler with that data. Maybe not all that fancy, but I think it’s cool :). Here are what the functions look like:

getXMLHttpRequestObject:

function getXMLHttpRequestObject()
 {
          if(Firefox)
          {
             return new XMLHttpRequest();
          }

          else
          {
             return new ActiveXObject('Microsoft.XMLHTTP');
          }
 }

sendXMLHttpRequest:

function sendXMLHttpRequest(Request, uri, POSTdata, responseHandler)
 {
          Request.open("POST", uri, true);
          Request.setRequestHeader(
                                   'Content-Type',
                                   'application/x-www-form-urlencoded'
                                  );

          Request.onreadystatechange = function()
                                       {
                                                if(Request.readyState == 4)
                                                {
                                                   if(Request.status == 200)
                                                   {
                                                      responseHandler(Request.responseText);
                                                   }

                                                   else
                                                   {
                                                      alert("There was a problem retrieving data: " + Request.statusText);
                                                   }
                                                }
                                       }

          Request.send(POSTdata);
 }

A sample call is as follows:

function doSomething(param1, param2)
 {
          var Request = getXMLHttpRequestObject();
          var FormData = "param1=" + param1 + "&amp;param2=" + param2;
          sendXMLHttpRequest(Request, "doit.php", FormData, myResponseTextHandler);
 }

 function myResponseTextHandler(text)
 {
          //do something with text
 }

Neat huh? Javascript is a whole lot more powerful than some people think. Ok, enough geek talk!

References:

You have 48 hours to stop the Broadcast Flag

Ladies and Gentlemen, you have 48 hours to stop the broadcast flag. If you remember, they tried this sometime ago, but it was struck down. What is the Broadcast Flag? Basically it is a way for the money-hungry bastards at Hollywood and the MPAA to control what you can view, and when. Today, you can easily download HDTV content to view at a time that’s convenient for you. If the broadcast flag is in effect, there are a few things that you will not be able to do. Restrictions can take the form of the following:

  • Flagged content cannot be saved. Even if you are able to save them, there will most probably be reduction in quality.
  • You will be unable to skip commercials. This is the same deal with the bullshit they show you in DVD’s these days. You have to sit through the previews and commercials before you can get to the actual movie.
  • You will not be able to burn downloaded content from the hard-disk into a DVD, if you wanted to save space.
  • Want to send the content to another node on the home network, or another TV? Nope. The broadcast flag won’t let you do that either.
  • Content can only be viewed by “authorized” devices. So suddenly you’ll have to replace all your hardware with ones that have been “blessed” by the assholes at the MPAA

End result? A big “fuck you” to consumer rights. Of course, the MPAA and Hollywood will tell you that this is all done to fight piracy. In addition to that, they will even tell you that you might be helping terrorists because of piracy, so of course, this is also helping “The War Against Terror”. It never surprises me how low these bastards can go. The broadcast flag is complete abomination. There is no way it will make a dent in piracy, especially large-scale. Current use of consumer electronics by American citizens to download and view shows at a later time, fall entirely within fair-use rights. If the flag is authorized, everyone will need Hollywood’s permission before they can create anything, or risk being sued. I can only see this stifling innovation.

This underhanded and cowardly tactic is not new for them. If you remember, they tried to sue VCR makers and MP3 player makers. If we had these kinds of laws 20 to 25 years ago, we wouldn’t have many of the things we take for granted today. Things like MP3’s and iPods. Hollywood and the MPAA have a right to distribute their content. But when that right encroaches upon my right to decide WHAT to do with that content, and how I decide to view it and when and where I decide to view it, I have a problem.

So what can you do about this? Go ahead and sign a petition, or call your senators. Make a stand now.

w00t! Article accepted!

I got another article accepted by Slashdot. That brings it to a total of 3 articles accepted (out of 7 that I submitted). In all honesty though, the first few times I submitted articles, I submitted them to what I thought was the appropriate category, instead of submitting them to the index. Maybe that was a factor in rejection. Yeah, this is a big deal for me because it’s Slashdot! And I get to see my name on the front page of a really popular site! My previous articles are here and here.

Revenge Of The Nerds

I ran into an article from the New York Daily News few days ago. I thought it was pretty interesting:

Nerds make better lovers

Ready for a real relationship? Ditch the
pretty boys and grab yourself a geek

By TRACEY LOMRANTZ

Sitcom siren Courteney Cox saw a prince charming in fashion freestylist David Arquette.
Golfers aren’t notorious studs, but Swedish stunner Elin Nordegren thinks hubby Tiger Woods has got game.
Super-chic Rachel Bilson fell for her slightly geeky co-star Adam Brody, on-screen and off.
Christina Aguilera recently traded in piercings for petticoats, apparently making the usual Marilyn Monroe morph. But there’s more than meets the eye: Sure, she’s blond, buxom and sweet-voiced now, but she’s also emulating the classic bombshell in matters of the heart.

You see, Aguilera’s fiance, like Monroe’s husband, playwright Arthur Miller, is kind of a geek.

Full Story

I looked at this article in two ways. My usual cynical way, and then in a sort of optimistic way. I was cynical because people rarely do what’s good for them. This is why girls keep talking about how they want to date a nice, sensitive, caring and good guy, but then they say “Oh instead, let me just date this asshole over here!”. Then they will come and cry on the shoulder of the nice guy about how terrible guys are and about how they can’t find any nice guys. This nice guy of course has a romantic interest in the girl. But the girl is not the only one to blame – you gotta blame the nice guy for being a glutton for punishment and sticking around with a girl like that.

This whole nice guy thing is a favourite topic for me to rant on, and is pet peeve. It defies all logical explanation – doesn’t make any sense. I mean, it’s like going to a car dealership that sells really great cars at really great prices. They are economical, have great mileage, have the latest features and gadgets, and rate extremely high on safety features. You talk to the sales consultant about how all you are looking for is a great car. Then you turn around and go to the neighbouring dealership to buy a flashy-looking car with terrible mileage, low safety ratings, and that will break down much, MUCH earlier than the one sold by the first dealership. Then everytime you take your flashy car to the mechanic to get it repaired, you stop over at the first dealership and complain to the salesperson about how terrible your current car is, and how you can’t ever find a nice reliable car. I mean come on – that certainly deserves an almighty WTF?! My only explanation is that some women are simply illogical and manipulative. They like having the nice guys around them because they get the attention, but they want the “bad boys” because of some misplaced sense of adventure and the equally misplaced belief that they can somehow “change them” and you know that’s just SOOO freaking romantic because then the guy changed his personality just for them!! Aww so sweet right? Yeah right. Let me tell you sweetheart – once a jerk, always a jerk. The good news is that some of these girls realize that their position is rather tenuous and immature – I guess it’s a part of growing up. I can’t say nice guys are all that blameless either. Some of them do need to develop a spine. Being nice all your life will get you nowhere, and people will take advantage of you. You probably have some sort of altruistic ideal and probably think that playing the martyr makes you a better person. Er. No. The world is a terrible, terrible place and sometimes you have to be tough. So get a spine and be a man. And like the illogical girls, the spineless nice guys also eventually develop a spine, and maybe it’s part of their growing-up process. So all will eventually be well in the world. Maybe.

Ok enough about the cynical and bitter view. My optimistic view was that maybe things are coming around. The fact of the matter is that very few people, much less women, actually know how geeks and nerds (most geeks/nerds are nice guys) think. Despite the cultural stereotype of the socially inept (and thus ostracized) nerd, a lot of nerds can hold their own in a social situation. It’s just that they do things differently. It takes a woman with patience to find that out. Nerds may not be as aggressive as that macho jock who will sweet-talk you, and then in the end be a total jerk to you. But they are able to provide – emotionally, physically, and financially (for the shoes! *duck*). Women may finally be realizing this. All it takes is just a little bit of patience and you can really see nerds for what they are – caring, sensitive, and loving individuals, albeit a little shy and reticent. In addition, nerds and geeks may have a mild form of Asperger’s Syndrome which would explain some of their idiosyncratic behaviour.

Anyway, I was talking about how nerds think differently – most of them approach things as if they were problems to be solved. This includes a relationship. This could be an issue, because it’s probably better to just “go with the flow” than to try and analyze everything. This attribute of “solving problems” extends to even emotional problems. Sometimes a partner will just want to talk about an emotional problem, without actually seeking any resolution. In this scenario the nerd will actively try to discern the cause of said problem and will then try to solve it – much to the frustration of the partner, who merely needs a sympathetic ear. What is also unfathomable to some people is the zeal with which nerds approach problems. If a nerd has a seemingly unsolvable problem, he/she will keep at it until he/she solves it, quite often ignoring everything else around them. This is also a source for frustration.

Bottom line – nerds are rich individuals. Just take the time to get to know them and understand them.

It’s hard to talk about this

I put off writing about this for a long time. Mainly because I didn’t want to think about it, and also because I wasn’t sure what I would really say.

My grandmother passed away last week. She was 72 years old. She was on life-support for a few days, but eventually she passed away – peacefully.

When I first found out, I didn’t feel anything. I don’t think I understood (and I still don’t completely) that she was gone. She had always been there for as long as I remember. I looked forward with anticipation every year to the day when the summer vacation started. That was when we would leave to go to my home town.

Some of my earliest memories of her, are of her teaching me different mantras and nāmams. Every evening, during the evening prayers, she would teach me one. Then in the morning, she would make me recite it to her. That summer I learnt a whole bunch of them. Sadly, I do not remember many of them now.

A few days ago, I sat down to eat dinner. I reached into my cupboard to take out a jar of dried mangoes. As I looked at it, tears welled up in my eyes. My grandma had made that for me. Dried mangoes are my favourite. Every time I’d come to India, she’d make sure she made some from that year’s crop of mangoes – these were made from mangoes that grew in our house. When she grew older and could no longer make them on her own, she had our house-maid make them, but she would supervise the entire process. I think that’s when I realized that she was truly gone. A little thing like that jar is what finally made me realize. But I know it is going to be harder for me when I go to India again… when I see her room… the pūja room…

The consolation is that she passed away peacefully, with my grand-dad, my dad, my mom, and my aunt by her side.

Whatever it is, she is in a better place now. And even if she isn’t here in this world now, she will live forever in our hearts and minds.

Scrolling problems

I’m having some problems with my Javascript code when it comes to scrolling with the mouse-wheel. You have to focus out side the “content” for it to scroll properly, especially in journal entries where the content is really long.

I have an idea in mind, but it involves restructing completely. I’m thinking of removing the iframe and dumping in content directly.

All original content on these pages is fingerprinted and certified by Digiprove
%d bloggers like this: