I would like to solve the following problem: Given a link on a Web page, I would like to replace the content of a specified div
element with the content of a div
element of another page. Say, just load the text "Stand on the shoulders of giants" from the Google Scholar page into my existing div
element.
Up to date, if implemented the following example:
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<a href="http://www.whatsmyip.org/">Click me</a>
<div id="myid">Text to be replaced</div>
<script type="text/javascript">
$("a").click(function() {
$.get(this.href, function(data) {
$("#myid").replaceWith($(data).find("#fb-root"));
});
previous_page = location.href;
window.history.pushState({page: $(this).index()}, $(this).html(), $(this).attr('href'));
return false;
});
window.onpopstate = function(event) {
location.reload();
}
</script>
</body>
</html>
I've included window.history
in order to change the URL in the location bar, and to allow the user to restore the previous state of the Web page without the new content.
Now, I have two issues:
- The replacement of the
<code>div</code>
element seems not to work, since the entire page from WhatIsMyIP is loaded. - While using Chrome, the entire page gets reloaded again and again right from the beginning. I think, the event
window.onpopstate
is triggered continuously.
Thank for any help!