views:

153

answers:

4

Hi!,

I download via jQuery AJAX a whole html webpage. I want to replace the content of the current page with the one downloaded via ajax. I do it with document.write(). It doesn't work correctly because whenever I try to modify the hash, the webpage is reloaded.

I know in IE it it necessary an iframe, but that is not the problem, because I use jQuery History plugin. The problem is due to the use of document.write(), but I don't know why.

Can anyone help me?

TIA.

A: 

don't use document.write().

instead use $('your selector').html(your_html_fetched_via_ajax);

Here Be Wolves
the problem is that it doesn't work when "your selector" is <html> root node. The content is a complete webpage, with <head> and so on...
David
in that case, consider using an iframe. see http://stackoverflow.com/questions/205087/jquery-ready-in-a-dynamically-inserted-iframe
Here Be Wolves
A: 

Hi (I'm the original post writer).

I will try to describe it in more detail:

index.php -> main entry point, which downloads JS code to parse URL after hash and invoke request.php.

request.php -> request entry point. It returns the webpage.

It works OK when I simulate a direct request to request.php and the downloaded webpage updates the hash.

It doesn't work (in FFox only) when I simulate a original request to index.php, which downloads the webpage via request.php and the downloaded page modifies the hash.

I use document.write() to write the content of the webpage to the current window. So the problem is about the modification of the hash in a document "being written".

Thanks in advance!

David
I think it would be better if you edit your question and write this as an update, instead of making it an answer post.
Tony L.
A: 

I thinkg that you can't modify the whole html object because it means erasing the reference to the javascript script tag. I would say your best bet is to either just link to the request.php page or just change the body tag

$('body').html(response_html);

And I agree with harshath.jr, don't use document.write().

Tony L.
A: 

The individuals pointing you towards an iframe are correct. Add the iframe, and simply set the src attribute to the page you're fetching...you won't even need request.php.

If you really want to try to load in the html without an iframe, you'd have the parse out the elements in the head and add them to your documents , and also parse the contents of the and add them to the current pages body. Its not guaranteed to display correctly, though. I think an iframe is really what you're looking for.

Skone