0

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.

Update:

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".

Community
  • 1
  • 1
David
  • 9
  • 1

3 Answers3

0

don't use document.write().

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

jrharshath
  • 25,975
  • 33
  • 97
  • 127
  • the problem is that it doesn't work when "your selector" is root node. The content is a complete webpage, with and so on... – David Dec 29 '09 at 10:14
  • in that case, consider using an iframe. see http://stackoverflow.com/questions/205087/jquery-ready-in-a-dynamically-inserted-iframe – jrharshath Dec 29 '09 at 10:21
0

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.
  • 7,988
  • 5
  • 24
  • 28
0

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
  • 745
  • 4
  • 13