javascript page redirection without page reload - javascript

I have a situation where you scroll down a page to a certain button that clicked will lead to a new page (in the same window).
If inside the new page I implement another button that via JS returns to the starting page using history.back() it has the benefit that the user lands on the exact (scrolled down) point where he clicked the first button.
New situation:
I’d love to keep this benefit, but I can’t use history.back() instead I have to use the src of the original page. Doing so by f.e. using window.location.href = "my_starting_url"; it goes back to the original page but at the top of the page. Why? Is there a simple method to achieve the same behavior as history.back() i.e. reopening the page at the same (cached) point?
Did I explain myself sufficiently?
Thank you so much in advance!

The way I see it the best option would be the History API ( http://diveintohtml5.info/history.html ): This will allow you to fetch a new page and store the scroll position of the current one, once the user clicks on "back" you can load the previous page and set the scroll position exactly where it was.

Related

Start page load while closing animations

Question: With Javascript, can you tell the web browser to begin loading a page, but don't begin rendering it yet?
Issue: A client wants his web page to show listings like a book. When you click on the next button, he doesn't want the next page to immediately load. He wants the book to close (a closing animation) and then load the next page.
Current status: All links go to Javascript. I show the closing animation. Then, I replace the window location. The issue is that there is a clear wait for the next page to load. It would be nice if I could load the following page into cache while the closing animation runs. In other words, I want to make that three-second animation useful time by loading all the HTML, CSS, Javascript, and images for the following page and then all that happens when I set the new location is that it renders.
Possible solution: I have the main page that had two full-screen iframes in it. One iframe is the current page. The other is hidden and is used to load the next page. After the animation, I flip which iframs is visible and which isn't. This is good except that the back button doesn't work properly. If you click back, you go to wherever you were before you went to the website. You don't hide the current iframe and show the one you just hid. If you click back twice, flipping iframes doesn't work. I have to keep a log of your history. Further, I have to hack the back button, which I don't like. So, I'd like to use a built-in cache method if I can.
Possible solution: I have the main page that had two full-screen iframes in it. One iframe is the current page. The other is hidden and
is used to load the next page. After the animation, I flip which iframes is visible and which isn't.
Yes, this sounds like a good approach.
This is good except that the back button doesn't work properly. [...]
I have to hack the back button, which I don't like.
Single Page Applications (SPAs) can't use the back button as originally intended because the entire application exists within a single document.
Partly in response to this, we have
history.pushState()
which is a really good extension to the History API, enabling new "artificial" entries (describing new states) to be added to the browser's history, which, in turn, enables the back button to work exactly as the user might expect it to.
Further Reading:
http://html5doctor.com/history-api/
https://developer.mozilla.org/en-US/docs/Web/API/History/pushState

How to keep page on refresh and navigation history

I am using this library to create my page navigation. However, since this sort of navigation is new to me, because all pages are in a single html file instead of different files, I'm unsure how to save that kind of history.
Using <a> name and href does not work in this situation, because it doesn't store the anchor for the page transition, rather storing the page index as data.
I'm looking for a way to:
A) Keep the data number (open page) active when I hit refresh (because out of the box it always goes to the first page)
B) Keep a navigation history, so you can hit the back button after navigating between pages
Any suggestions are helpful, as well as maybe some basic examples to work from. Thanks!
You can use the History API to manipulate the history on .pt-trigger clicks
$('.pt-trigger').click(function() {
var stateObj = { foo: "bar" };
history.pushState(stateObj, "Your page name, "bar.html");
});
Which changes the URL to foobar.com/bar.html when a page transition trigger is clicked. For the stateObj, you'll need to save some identification of the previous page, so you can transition back with the back button by using onpopstate event.

Hijack Back Button? [duplicate]

I am trying a new functionality for my web site. I want to do simple navigation by hiding/showing <div> elements.
For example, when a user clicks a "details" button on some product, I want to hide the main <div> and show the <div> containing the details for the product.
The problem is that to go back to the previous "page", I have to undo all the display/visibility style changes, which is ok if the user clicks the "close" button in the newly opened <div>. But most users will hit the BACK button.
Is there a way to make the BACK button go back to the previous "state" of the page i.e., undo the visibility/display changes?
Thanks.
Yes. What you're looking for is called AJAX browser history.
There are a few open implementations out there, like RSH as well as plugins/modules for frameworks like jQuery and YUI.
to answer the question of your title (that's what I was looking for)
Using the BACK button to revert to the previous state of the page
and from the link from #reach4thelasers's answer, you have to set up a timer and check again and again the current anchor:
//On load page, init the timer which check if the there are anchor changes each 300 ms
$().ready(function(){
setInterval("checkAnchor()", 300);
});
because there's no Javascript callback triggered when the BACK button is pressed and only the anchor is changed ...
--
by the way, the pattern you're talking about is now known as Single Page Interface !
You need to add an anchor to the URL whenever a change is made
www.site.com/page.html#anchor1
This will allow the browser to maintain the pages in its history. I implemented it in my current site after following this tutorial, which works great and gives you a good understanding of what you need to do:
http://yensdesign.com/2008/11/creating-ajax-websites-based-on-anchor-navigation/
Your example in the comments won't work, because it works like this:
Page Loaded
Page Changed, Add Anchor to URL (back button takes you back to back to 1)
Page Changed, Anchor Changed (back button button takes you back to 2)
Page Changed, Anchor Changed (back button button takes you back to 3)
.... and so on and so on..
If there is, it sounds like a pretty evil thing to do from a UX perspective. Why don't you design a "back" button into your application, and use design to make it obvious to the user that they should use your application's back button instead of the browser.
By "use design," I mean make your application look like a self-sufficient user interface inside of the browser, so the user's eye stays within your page, and not up on the browser chrome, when they are looking for controls to interact with your app.
You can do this with anchors, which is how it's done in a lot of flash applications, or other apps that don't go from page to page. Facebook uses this technique pretty liberally. Each time the user clicks on a link that should go in their history, change the anchor on the page.
So say my home page link is:
http://www.mysite.com/#homepage
For the link that works your javascript magic, do this:
My Other Page
This will send the user to http://www.mysite.com/#otherpage where clicking the back button will go back to http://www.mysite.com/#homepage. Then you just have to read the anchors with
window.location.hash
to figure out which page you're supposed to be on.
Take a look to this tutorial based on ItsNat a Java web framework focused on Single Page Interface web sites

Implementing backbutton with infinite scroll

I have new pages loaded via infinite scroll on my site. Each time a new page is loaded I update the url using HTML5 pushstate API:
if(history.pushState) {
history.pushState(null, null, '/page:' + current_page);
}
My urls looks like this:
http://mysite.com.com/page:1
http://mysite.com.com/page:2
http://mysite.com.com/page:3
and so on...
When the user clicks on an item in the listings (for example on page 2) and then clicks the back button he is taken back to:
http://mysite.com.com/page:2
But instead of being at the top of the page, he is at the portion from which he clicked. Since page:2 is now loaded at the very top, the part where he is loads anew
page 3 and he is looking at a post from that page instead.
Is there a way to make the user go back to the top of the page each time the back button is clicked?
I tried using anchor tags and javascript scroll on page load but neither is viable since there are several jumps before the user is taken to the top.
The best example of backbutton working with infinite scroll that I have seen is http://imgur.com but I can't figure out how they do it.
There is a library called infinite-ajax-scroll on github that seems to do what you are looking for. You can initialize it using the history option, which will update the hash of the url as you scroll the page. The only issue is that you'll have to change the way you load your data to work with the library.
jQuery.ias({
....
history:true,
....
});
You could also check out this blog post on tumbledry.org, which explains something closer to what you are trying to do, but I don't think its implemented as well.

How can I get links to a redirect page to show up in the browser's history?

A website contains a "random" link, which loads a url that returns a 307 redirecting to the url we want. It works fine: click it and you load a random page. The problem is that each time you click it, the browser assumes you're loading the same page: so if you're on the homepage, then you follow the random link 5 times, then you press back, you'll be taken all the way back to the homepage, with no way to find the random pages you were just looking at. I want to modify this behavior so that users can access previous random pages via the back and forward buttons.
I don't own the website, so I can't just change the redirect code.
Here's what I've tried, all of which has failed.
Predicting what would be redirected to. While somewhat possible, there would be no way to avoid failure in up to .1% of clicks, and it would react very poorly to unexpected events, like a page that's published a day late, let alone a sit structure change.
Loading the 307 page via ajax. The request stops at readystate == 2 and I can't access the location header.
Cancel the click event and instead set location.href = random_link.href. This has no effect - the new page still doesn't go into history.
Have the new page call history.pushState. This successfully adds the page to history, but I can't find a way to distinguish between new pages and ones being opened via the back button, so the history quickly becomes very corrupted.
Keeping my own history in localStorage. As above, I can't tell when the back button is being used.
I'm working on a solution that I'm pretty sure will work, involving loading the page in an iframe over the existing page and using a background process and messaging to work around the fact that content injections from chrome extensions can't access window.parent from within iframes. And using the history API to reflect the current iframe's URL in the address bar, and get the back and forwards buttons to apply to the current iframe where appropriate.
While I'm pretty sure the last solution can be made to work, it's a hideously complex and heavyweight approach to what seems like a simple problem. So I thought I'd ask you guys before I continue: any other ideas?
Have you tried storing the locations in localStorage, then hi-jacking the back button ?
I am sure you know how localStorage works, for hi-jacking the back button you can refer to this : Is there a way to catch the back button event in javascript?
T.

Categories

Resources