Scenario: I am using jQuery to lazy load some html and change the relative href attributes of all the anchors to absolute links.
The loading function adds the html in all browsers.
The url rewrite function works on the original DOM in all browsers.
But
In IE7, IE8 I can’t run that same function on the new lazy loaded html in the DOM.
//lazy load a part of a file
$(document).ready(function() {
$('#tab1-cont')
.load('/web_Content.htm #tab1-cont');
return false;
});
//convert relative links to absolute links
$("#tab1-cont a[href^=/]").each(function() {
var hrefValue = $(this).attr("href");
$(this)
.attr("href", "http://www.web.org" + hrefValue)
.css('border', 'solid 1px green');
return false;
});
I think my question is: whats the trick to getting IE to make selections on DOM that is lazy loaded with jQuery?
This is my first post. Be gentle 🙂
Thanks,
Joel
If you have ever wanted to lazy load some HTML using jquery .load() you need to know that IE and FF don’t treat relative HREFs the same way. IE adds a ghost domain to the relative URL (to the lazy loaded html).
As a result, if you load a DIV of content full of HREFs you cannot count on using $(a[href^=/]) to find all relative HREFs. IE won’t have any.
So I wrote this:
(Bear with me, I’m a CSS guy who does some jQuery. I know this could be written MUCH more elegantly. Could someone help me out with that?)
Perhaps lazy loaded HREFs in IE don’t act this way for all people all the time… and I just found some niche circumstance. I don’t know. Hopefully this HREF post will save someone some time.