Reputation: 3913
I know how to write HTML to a new window with window.open()
but that causes a new window of course.
I'd like my HTML output string to be written to the current window. The effect being just like clicking a link. The purpose is to ensure I have a browser history.
When I refer to a URL using window.location.href()
, I cannot write content, since the href attribute has to be a URL.
Is there a way to combine the two, and get a new page loaded into the browser history that uses a string value for the HTML instead of a file? No server-side involvement allowed.
Upvotes: 0
Views: 352
Reputation: 3556
HTML5 brings with it a new History API for JavaScript. This allows you to push things to the browser history using JS. With this you might be able to accomplish what you are looking for. If this doesn't quite help, please clarify your question, as it's a bit hard to tell what you're asking for.
Edit:
I think this is what you are looking for. Take a look at this example. When you click a link such as "fluffy", the site actually just replaces the text of the body of the site, and adds an entry to the browser's history to make it appear that the user actually went to a different page. In reality, the browser never made another request, never opened another page, and it is all just javascript trickery to make it appear that way.
Explaination of the demo link I sent you, with lots of good info:
http://html5doctor.com/history-api/
A demonstration in jsFiddle:
I've made a small demonstration of what I am talking about here in a jsFiddle. If you look, you will see a link. If you were to right click and view the source of that little square containing the content, you will see that you are viewing the source of the page "http://jsfiddle.net/ByZpW/4/".
Click the link.
Now you will see new content, and new text on the link. If you were to view the source of that little square again, you'd see that you were viewing the source of the page "http://fiddle.jshell.net/lolnewpageonjsfiddledomain/".
Clearly jsFiddle didn't let me add a new page to their domain, so what happened? I told javascript that I went somewhere else and it believed me. Now if you click into the content square and hit backspace you'll see that you are taken back to the "first page". If you view the source, you will confirm that you're back at the original location. Of course this is all a great big lie. We didn't go anywhere, and everything was made to look that way with JavaScript.
Pay attention to the line:
// History magic here
history.pushState({link: link, content: content}, "test", $(this).attr("href"));
That's where we use JS to say "Pretend I went somewhere else, even though I'm still right here".
Upvotes: 2