In JavaScript world, if I click on a button on page A and it opens up a new tab (calling it page B), is it possible to get page A's browsing history from page B? In my case there isn't a way to make changes on page A, so sending data via query string or something like that is not doable. All I'm needing is to get the url that page B comes from. Thanks!
If I understand your question correctly and you want the url of the page A that sent you to page B then you can get the referrer to the site with document.referrer.split( '/' );.
If you want to, as your phrasing may imply, access the history of the browser that is not possible via common Javascript.
Seems like you could achieve this via document.referrer, which will give you the URL of the site that loaded the current document:
https://www.w3schools.com/jsref/prop_doc_referrer.asp
I'm by far no JavaScript expert, but from what I've read it isn't possible to get the history of the browser itself and the history api like history.back() and history.forward() only allow you to navigate forward and backwards.
You can use the back() method in javascript.
function getHistory(){
var $history = window.history.back();
window.open($history, '_blank');
window.focus();
}
<input type="button" value="Click me" onclick="getHistory();">
Edit: NOTE: Opening new tab is not that easy. You can read this
Related
How to replaces the current URL document with a new one but removes the URL of the current document from the document history, meaning that it is not possible to use the "back" button to navigate back to the original document and not store in browser history list.
I can to this by jQuery or javascript by
window.location.replace("newURL");
but I need to do like this in php.HOW?
If you do a <?php header('Location: http://www.abc.de/'); ?> on the page http://www.something.de/ then the page http://www.something.de/ will not be visible in the browsing history nor can the user navigate back. Is this what you are asking for?
If you want no remnants of the page in the browser history at all, then you cannot send the browser there because it will keep track of all visited pages in the history so it can properly show which links have been visited. As best as I know, there is no way to stop the browser from keeping track of the visited links. If you do a 302 location redirect from the server, both original and redirected page will be in the browser history. The original page will not be in the back-button list, but will be in the history.
If the part of the URL that you are trying to keep less visible is query parameters to the URL, then you can use a form post instead of regular page load because the browser will not store form parameters in the visible part of the history.
Otherwise, you may need to use your URL with an Ajax call (since that will not be stored in the history) and then use client-side window.location.replace(...) to go to the final URL, the idea being that the URL you don't want to share publicly will have never been an URL that the browser page went to, only a URL that was used for an Ajax call.
It worked for me
<button onclick="goBack()"></button>
<script>
function goBack() {
window.history.back();
window.location.replace('url');
}
</script>
If you want to clear your previous page, try to use "history.back();" and then set your windows.location.
I was going through the history back() method in Javascript at W3School's website. I was wondering if its possible to go back in history in a new tab.
Lets say I google search "Liverpool fc" and open a website using open link in new tab
Now when the liverpool website opens in a new tab is there a way to go back to the google search?
The below function wont work:
function goBack() {
window.history.back()
}
Is there any way out?
No, it isn't.
That page isn't part of the current window's history.
That is why the browser's back button wouldn't work either.
You can send the url(window.location.href) to the new tab and in the new tab use the history api to push the url to the history state. Look here: Working with the History API
Edit: Misundersood your question.
So, if you want to create this on you own, it is possible to give the url you are opening in the new tab an attribute with the referrer url. Something like this:
http://yourpage.com/?referrer=http%3A%2F%2Fyourpage.com%252Fsublink
Otherwise there is no possiblity for what you want to achieve.
THIS IS NOT THE SOLUTION, JUST DOESN'T WANT TO WASTE IT
It actually is possible. Just take a look at the document object, and you will find a referrer attribute. It's the URL you are coming from.
If you want to open a new tab you should take the workaround from duke that looks like this:
function OpenInNewTab(url) {
var win = window.open(url, '_blank');
win.focus();
}
After this, you can create a new link with an onclick attribute:
<a onclick="OpenInNewTab(document.referrer);">Open last in new tab</a>
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']
I'm stuck using a CMS that only gives me the ability to modify the content of the <body>, so when I want to redirect people, I've used this
<script type="text/javascript">
window.location = "http://www.example.com/"
</script>
So, yes, the page loads first, and then 5ms later, the redirect happens, and it's worked for all intensive purposes. Now, I'm wondering if I can use javascript to do something else.
Is it possible to open a new browser tab, with a specified URL, and then redirect the user back to the previous page, through Javascript?
Many thanks, SO.
EDIT - Whether it opens a new window or tab, to be honest, is not as important as it actually functioning. I need Javascript to determine the prior page (if possible), then open a new window/tab to a URL I specify, and then redirect the current window/tab to it's prior page. Some are saying that window.open only works on a click event, which will not work for what I am trying accomplish either... just fyi.
So, literally, without clicks, I need Javascript to do the following -
Determine the prior/previous/last page the user came from, store it as a variable
Open a new window or tab, to a specified URL
window.location back to the prior page, which I stored as a variable
Hope that makes sense.
Depending on the user's browser setting using window.open can open the new window in a new tab instead but you CANNOT directly control this through the browser. It is all down to the user's settings.
To open a new window:
window.open("http://www.google.com", "windowName", "window options (optional parameter)");
Then simply use:
history.back();
You can also use the referer property:
var previousUrl = document.referrer;
For more info on window.open, see: http://www.javascript-coder.com/window-popup/javascript-window-open.phtml
For more info on the document.referrer property, take a look at: http://www.netmechanic.com/news/vol4/javascript_no14.htm
What I have is a MVC framework that I am manipulating the DOM via JavaScript and jQuery. Which to a point is a mute reference to this question. Through the MVC if I go to
domain.com/page/
I land on a default page with recent info, updates, etc. However through the MVC if I go to something like
domain.com/page/ref1/ref2/ref3
We can handle that on the backend to do what we want. When its gone to directly. So the thought here for this question is, can I mimic the behavior cross browser up to at least up to IE 6/7+, Firefox 3.x+, Chrome. And by mimic I mean manipulate the URL/URI like I would the DOM itself. This way when someone comes along and navigates to sections via the methods we manipulate the DOM and find something they want to share all they have to do is go up to the browsers URL bar, and then copy and paste it to a message.
What would be even nicer is a method that can work with the back and forth history buttons on browsers as well. All without having to reload/refresh the page.
have you tried
window.location.replace(URL)
This function doesn't load any pages.If you change your current location like this , when you redirect to somewhere , your last address will be the non-modified address.It doesnt write new URL to the browser history.So it would be like as if you just give the key to what the user wants to share.
Have you considered modifying the anchors in the URL? While on this page:
http://www.google.com/
Changing the URL to this:
http://www.google.com/#foo
... won't trigger a page refresh. So you could use (read/modify) the contents of the URL after the anchor all you want and the page won't get reloaded. Should be able to build on that idea.