Posts Tagged ‘jQuery’

IE7 HREF Normalization and AJAX

Wednesday, August 18th, 2010

I have often relied on jQuery’s awesome cross-browser compatibility tricks. One of these is fixing an inconsistency with how link HREFs are reported in IE compared to other browsers. In most browsers, when asking for the HREF of a link, JavaScript sees what is actually in the document source. In IE, however, the HREF attribute is “normalized” into a fully-qualified URL. This can be a problem.

Generally, jQuery fixes this for you when you use the .attr() method to retrieve the “href” attribute. The returned value is whatever is in the source code, not the normalized URL, regardless of browser. Success!

Unfortunately, I discovered today that is not always the case. In this specific scenario the normalization still happens:

  1. Open IE7.
  2. Use AJAX to load some content into the page that contains links.
  3. Examine the link HREF using .attr(‘href’).
  4. See that the result is a normalized URL. Oops.

I haven’t yet performed reductions to see if the problem is the AJAX load or the DOM insertion, but it only affects IE7, not IE8. At this point, my workaround is to do the regexes manually to extract the part of the HREF I want, rather than relying on jQuery. Clumsy, but it works!

Dynamically Adding JavaScript Files

Monday, April 13th, 2009

I recently discovered an interesting feature of JavaScript. Perhaps this is obvious to some. But JavaScript can dynamically add JavaScript tags, and then utilize them. A hang up of JavaScript has always been the inability to specify dependencies within a single JavaScript file. But this doesn’t need to be a problem. JavaScript can print out new JavaScript tags, dynamically if necessary, fulfilling any requirements or dependencies. A great way to avoid JS errors. Using jQuery:

// Dynamically adding js files
//
$("head")
  .append("http://www.rapiddg.com/scripts/test.js");