I have a document with one CSS linked internally. How can I replace the current CSS with one coming from the document I just received with an AJAX request using jQuery?
Here is the code I'm trying to do right now, but have not yet succeeded:
$(function() {
$.get('next_page.html', function(data, textStatus) {
$('link[rel=stylesheet]:first')
.attr('href', $(data).find('link[rel=stylesheet]:first').attr('href'));
});
});
Update: $ .find () does not work in any browser (tested by Firefox 3.5, Chrome and Safari 3 on Mac), but $ .filter () found only the stylesheet in Firefox 3.5 - still nothing in Chrome and Safari 3.
It should be simple, correct - replace the current CSS href with a new one, and voil ??
For some reason, jQuery does not find anything inside a tag <head>that comes from an AJAX request. In addition, jQuery cannot even find all <head>of the AJAX data. In other words, $(data).find('head').size()inside the callback function returns 0.
I am using jQuery 1.4.
UPDATE February 10, 2010. I sent an error about this to jQuery and they agreed that it was not possible to find any of the tag <head>from ajax data. Here is the answer I received:
http://dev.jquery.com/ticket/6061#comment:1 - "Yes, that’s right - parsing direct HTML, we guarantee only the content of the body element. If you want to access the XML page of the page right then I recommend you explicitly make the .xhtml file or request it as XML, for example: "