views:

186

answers:

2

Hello, I'm using a simple piece of code

$("container").load(url, function(){ callback();})

to replace the content of a div container using jQuery's AJAX functionality. That appears to work well with Internet Explorer, however after trying to view it through Firefox I am getting some unexpected behaviour.

The page is fetched and placed correctly inside the div only for a moment; then the whole page appears to refresh automatically and seems to redirect itself to a page prefixed with

wyciwyg://1/http://
Needless to say that it just hangs there with a blank page and nothing happens unless I click on Firefox's stop button. Are there differences between IE/Firefox that could be causing such a behaviour in jQuery? Thanks in advance for your help.

A: 

There's probably <script> tags in the document returned at the targeted url.

Loading content into the page that contains <script> tags has browser specific quirks that jQuery cannot hide (well... it tries, but in the process makes the quirks even more quirky and strange). You should avoid fetching a page that contains <script> for use as HTML as with load(). Which means generally don't load arbitrary pages containing any old HTML constructs, but only addresses that are intended for use with jQuery content loading.

Whilst it's difficult to say why a script in the target page might end up replacing the page with a wyciwyg URL without some code to play with, it has happened in the past with calls to document.write, which is a behaviour that will definitely cause breakage in general as you can't write to document from an already-parsed document like the one you'll be loading into. It could also be an interaction with an add-on.

bobince
Thanks for your answer bobince, the target page does indeed contain <script> tags. In such a case I must assume that when jQuery evals the newly fetched js a refresh is forced causing the momentarily correct page to be replaced by a blank one. Now where to start looking..
heeboir
Looking for `document.write` would certainly be my first tack. But in general you don't want to be loading pages with `<script>` in. That way lies madness.
bobince
I disagree with bobince. There is nothing wrong with loading a page containing `<script>` tags if you take care to understand what is going on, and it's often unavoidable.
noah
See eg. http://stackoverflow.com/questions/1661224/stop-ie-from-loading-dynamically-included-script-twice for a hint of the difficulties involved.
bobince
+1  A: 

If document.write is your problem, try writeCapture.js (full disclosure: I'm the author). It has a jQuery plugin so include it and change your code to:

$("container").writeCapture().load(url, function(){ callback();});

and see if that doesn't fix the problem.

noah