On javascript how would you go about keeping a selected navbar?
I have been able to achieve this using sessionStorage but the issue is, if the user navigates the site directly on the url, then it wont register and the site thinks the user is still on another view. Is there any way to capture the place on the url the user is instead of capturing the navbar element he clicked?
You can use the window.location object, it provides good amount of information about the url that was loaded.
More info here:
https://developer.mozilla.org/en-US/docs/Web/API/Window/location
Related
When the user loads the page, I immediately do a window redirect to another location.
The problem is, when the user clicks back, it'll go back to the page which does the redirect.
Can I "cancel" the history of the previous page? So that when the user clicks back, it goes back TWO pages instead?
Instead of using window.location = url; to redirect,
try:
window.location.replace(url);
after using replace() the current page will not be saved in session
history, meaning the user won't be able to use the Back button to
navigate to it.
You can use location.replace to replace the current location entry (the redirect page) with the new one (the target). That requires that you do the redirection via JavaScript rather than with meta tags or a 302. E.g.:
// In the redirecting page
location.replace("path/to/target/page");
Live example | Live example source
For anyone coming across this page and looking for an AngularJS way to accomplish this (rather than javascript), use the $location service's replace() method (documentation) :
Use $location.url('/newpath'); or $location.path('/newpath'); as you normally would to do the redirection in angular. And then just add $location.replace(); right after it. Or you can chain the commands like this:
$location.url('/newpath').replace();
I want to be able to send a user back to the URL they came from after looking at a map gallery I'm building. document.referrer would work perfectly except that there are links within my gallery that change the initial document.referrer URL. Is there a way to set document.referrer once, on the initially entry, without it changing each time a new URL is loaded?
You'll need to store it somewhere on their first visit. In a cookie or localstorage, if you're restricted to client-side code.
If the url is unknown that you want to reach you want to use javascript's history.go(-lengthofitemsingallery)
If you know the url that you are sending them and that is where they came from then you want to use history.go("Url")
If they haven't gone there yet the you want to do a javascript redirect by opening a new window that opens in self like so:
myWindow=window.open('URLTOPAGE','_self','')
You could also do
LocalStorage['Home'] = document.URL
//end of gallery:
document.location=LocalStorage['Home']
Is it possible to go in previous page if i opened a page in another tab from search results (only using javascript or cookies)?
I have search page like test.com/search And search result like this:
1) test.com/search/a
2) test.com/search/b
3) test.com/search/c
Now if i opened any of search result link in new tab test.com/search/b (will be in new tab and i want to go on test.com/search page)
You can store text in cookies. You can redirect to different locations using javascript.
Write a cookie with the URL you're on, store it, let the user click on another page, hook into the page back event, read the cookie and change the location to the URL you read from the cookie. It is also possible to manipulate the hash part of the URL using JavaScript and to store, for example, JSON encoded information in it. With that it is possible to have bookmarkable URLs which refer to a specific state. This is also used in some [jQuery](http://tkyk.github.com/jquery-history-plugin/ plugins.
However, hooking in the back button event is not possible without HTML5.
If you decide to depend on HTML5, you can manipulate the browser history directly.
Is it possible to have a button on a webpage that will be named 'Back' and do window.history.back() if the user has navigated to the page from another page on your website and otherwise have some other title and be a direct link if the user navigated to your page from another website or went to the page directly.
Google plus on mobile seems to have this behaviour. When you click on a post in your stream then it has a 'back' button on the post page. However, if you go to the post page directly then it has a 'stream' button on the post page.
This seems tricky to implement because you don't have access to the urls in window.history.
Have you any chance of adding an ext lib like BBQ? It's a package used to manage the history behavior in your page.
I have done this before. You can do this with an anchor in the link. The anchor needs to have every get parameter of your application (i.e. application state) stored. Your application should be able to parse the anchor. To intercept the back button look here: stackoverflow.com/questions/136937/is-there-a-way-to-catch-the-back-button-event-in-javascript.
#benmmurphy I also had the same problem, then I used the following, which worked perfectly for me. You have to paste it on the page, from where you want to go back.
GO BACK
Hope this will help you.
I am making a simple online application.
I have a navigation bar with a few buttons and one "div" into which all the new contents will be loaded dynamically i.e. when I click "About", it will load the page parts into the "div" without reloading the whole page with .load() function. The problem is:
what if I need to give a link to the Documents section of my web site, or the user wants to store this kind of link, the url is not rewritten when browsing my site. when the user stores the default link it will always link him to the default "Home" part.
How can I rewrite the URL and what is the most correct way to do this?
As option you can use location.hash.
E.g. user clicks About
and you're loading the content of your "About" page.
Also the URL in browser's address bar will be changed to something like http://mysite.com/hello.jsp#about.
So now user can copy this URL and visit it.
Then in $(document).ready() you can check the location.hash, find that '#about' anchor in it and then load an appropriate content.
Another option is to use Backbone.js or any other JavaScript MVC framework that you like which will help you to build rich client-side application and will handle such things for you.
According to me, appropriate method is to update the hash of the URL. Something like example.com/#About and etc. People can bookmark these. You have to take care to make a AJAX call when you read a hash tag in the URL and load the respective page.
What I would do is make ajax call on hashchange event instead of click. This event is supported from IE8 to all modern browsers. If you want to support IE7 use the hashchange plugin by Ben Alman.