jQuery, ajax, IE, absolute url, recall after a first replace (second time)
Hi
I used the Jquery ajax content plugin ( http://www.andreacfm.com/index.cfm/jquery-plugins
), wich itself use the ajax behaviour of jquery.
I use ajax to replace a whole div. As there are some ajax links inside
the div, I re-ajaxify them after the div is replaced, using
ajaxContent.
This works fine with Firefox, Safari, Konqueror, but in IE 6-7 I got a
strange behaviour: the first "ajaxification" of the link works well,
but the re-ajaxification does not work well.
Investigating, I realized that IE interprets all the urls coming back
from an ajax call, and includes the host into them.
i.e : "/my/relative/url.html" will become "http://myhost.com/my/
relative/url.html".
This cause jQuery to interpret the second ajax call differently. In
jQuery-1.2.3.js, at line 2631, there is a test:
// If we're requesting a remote document and trying to load JSON or
Script with a GET
if ( (!s.url.indexOf("http") || !s.url.indexOf("//")) && s.dataType ==
"script" && s.type.toLowerCase() == "get" ) {
This test will not be passed the first time, and it will be passed the
second Time in IE only, causing this strange behaviour.
- The solution I adopted is to always remove the host before doing the
call.
- A better solution would be to modify jquery so that it checks wether
the host is the same as the one in the url we're coming from, rather
than just check if url contains (http) or (//). This will make IE
behave the same in the first and second ajax call
Anyway, what caused the strange behaviour was the fact that the "X-
Requested-With" header wasn't set if the test
was passed. And I needed this header for my application to work
correctly (I'm using Ruby on Rails). So, maybe a good idea would be to
add this header in all case, may this test be passed or not.
Regards
Sebastien.