Javascript/Jquery : get document referrer, when clicked browser's back button - javascript

Situation:
I have page1.html, on which i need to get document referrer
For example, i go to page0.html, then go to page1.html
On page1.html there are list of subpages
I click on some of them and go to page1_sub1.html
Then i click browser's back button and go to page1.html
On page1.html i need to get referrer (page1_sub1.html)
but document.referrer actually shows as page0.html
So, is it possible to get document referrer if clicked back button of browser or user go back via referrence like
« Back

No, that won't work. If needed, you can use cookies to store the history of pages users have opened—but that's complicated, and will fail if the user has your site open in different tabs.

Related

Changing tabs and landing back on the same tab?

Ok, I do not have code for this so I would need assistance here. I have a page that has multiple tabs worth of information. If I proceed to an entirely new page (URL) and then hit back on the browser, is there a way to land back on the tab I left off of?
when you click on a tab you can append a url parameter e.g. ?tab=tab1. And when you load the page you can check you're url parameters and default to that tab if it is in the parameter.
Here's an example that's similar. When you click on the toolbar on the left on bootstrap's getting started page it appends that item to the url. so when i click on 'support' the url looks like http://getbootstrap.com/getting-started/#support and when you go to that url it automatically scrolls you to that section on the page.

how to check window.history.back has value in javascript

i am creating a page with back button in corner. if the page launched for first back button should hide. if user navigates through page back button should show.
i used the code
(window.history.length > 1)?$("#back").show():$("#back").hide();
but my problem is,
window.history.length is keeps increasing.
But my requirement is when there is no page to go back then back button should hide again. how to achieve that?
you have more problems. what if the user came from google? there would be window history already. What if a user went to your site, then went to google, then came back?
If it's just a nav system as described by #Marc B, it's easy, you know where the user came from and how to get back (don't use the history directly). But if it is a back button no matter what link the user clicks into your site... it gets more complicated. Due to privacy restrictions, you can't access the user's actual URL history (just the length) and you can't access whatever url they are navigating to if you use capture the window.unload event. So you're left with limited options.
One option would be to record the window location on every page load to an array that you keep in either local storage, or a cookie. Then you should use this array to manage your history state rather than relying on window.history. When the user clicks your back button, you just pop off the last url on your site they visited and navigate them there. Keep in mind the first page load will put an item into your history array, so depending on where you check to see if you should show or hide your history button, you'll compare the length of your history array to either 0 or 1.

Iframe submits and user is redirected back to original page, but iframe still appears to be present?

I have some JavaScript that spawns an iframe which requires the user to fill out some details. After they submit the iframe form, they are redirected to where they were previously; however, the iframe appears to still be present as right clicking in firefox brings up "This Frame" options. In addition, I am unable to click links or anything on the page.
On the other hand, if I include a function that closes the iframe after the form submits, the user is again redirected back to the original page, but there are no right click "This Frame" options this time and all the links on the page can be clicked.
Can anyone explain to me what is going on here? Thanks!
If using Javascript to redirect, try adding this in front of the redirection:
window.top.location.href = "http://www.site.com";
Else, use target="_top":
<form action".." target="_top">
</form>

Is it possible to get URL of previous page visited from browser using javascript

I am on page A.
Now I click in page A and redirected to page B.
Now I click in page B and redirected to page A.
Here i want to know the url of page B.
I have tried document.referrer but its not working.
document.referrer works when a user clicks on a link to navigate to the current page but I don't think it works when you do a manual redirect in a Javascript function.
The only way I can think of is to store the URL of the page before the user is redirected to your current page.
You could store it in a hidden form or a cookie.
This question has also been asked before and its worth having a quick read through here. How do you get the previous url in Javascript?
I took this cookie example from it to show you what I mean.
$.cookie("previousUrl", window.location.href, {path:"/"});
You can't know the URL of the previous page, but you can link to it using
window.history.back();
or
window.history.go(-1);
if you want to know if there is a previous page, check
window.history.length;
If it's more than 0, there is a prev page. See https://developer.mozilla.org/en-US/docs/DOM/Manipulating_the_browser_history

How do I insert an entry into browsing history via JavaScript

How do I insert an entry into browsing history so back button goes to different page 1st click then original page on 2nd click?
So if you need a good explanation of what I want done, go to:
https://secure.exitjunction.com/howitworks.jsp
I just need a script that will allow me to insert an entry in the browsing history so when back button is hit, the user will be taken to my special page.
Here is a link to a jQuery Plugin:
jQuery Plugin
You can't directly manipulate the browsing history.
Such a feature would be seen as a security hole (and it would be), so I doubt that any browsers would ever implement it.
You might be able to hack around it however by doing something like this:
NOTE: This entirely hinges around the assumption that the referrer will get changed by the back button. I don't think this actually happens, so it more than likely won't work, but hey.
You have two pages, PageA and PageB.
The user hits PageA
The page (on the client, using javascript) checks the HTTP referrer, and if it is not PageB, then it immediately redirects the user to PageB.
Now that you're on PageB, if the user clicks the back button, it will go back to PageA.
PageA will check the referrer, it willmay be PageB, so there is no redirect.

Categories