Javascript : History back() Method - javascript

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>

Related

JavaScript retrieve the browsing history from a new tab page

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

Get previous page URL, Open New Window, then Redirect through Javascript

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

How can I refresh a tab from another using Javascript

I'm looking to achieve something like Wordpress does when you create a new post. It allows you to preview your post by opening a new tab. If, after that, you edit the post and preview it again, rather than opening another tab, it refreshes the previously opened tab (provided it is still open).
From some research it seems I can open a new window and give it a name to identify it, like this:
window.open(url,"foobar");
But, how can I later refresh this tab? location.reload() does not seem to take a name as an argument.
If I remember correctly this is the way to do it:
var win = window.open(url, "foobar");
win.location.reload();
I think you're looking for the property window.opener. See http://www.w3schools.com/jsref/prop_win_opener.asp
You can do this by "storage" event. for more details follow
Communication between tabs or windows
https://developer.mozilla.org/en-US/docs/Web/Events/storage

JavaScript Bookmarklet to open new Window and transmit params of origin Page

I have a perfectly working bookmarklet to grab the Document-Title and the URL of the current Website and place it into the URL of the new loaded Page (Code below) … now I wonder how to let this domain.com/bookmarklet?… open in a new small window (600x600px), so that I still see the old website from where I grabed the title and url in the background and have the new page (domain.com/bookmarklet?…) in the foreground.
javascript:location.href='http://domain.com/bookmarklet?url='+encodeURIComponent(location.href)+';title='+encodeURIComponent(document.title)
How can I achieve that?
Use window.open(...) instead of setting location.href = ....
you need to use window.open() for that
javascript:window.open ("http://domain.com/bookmarklet?.......","MyBookmarklet");

link to itunes (itms://...) without opening a new window

how do i call a link to itunes (itms://...) without opening a new window.
with window.open there comes a new window (as the function says :))
$.get('itms://...'); doesn't work.
Any other possibilitys :)?
would be thankful for help!
Didn't try, but two ideas come to mind:
iframe
set location to the URL, it shouldn't replace your page anyway.
To clarify, you are referring to not opening a new browser window, as opposed to trying to open iTunes within the browser.
In that case, Nickolay's 2nd option is your best bet - this JavaScript will work (using actual example URL):
var url = 'itms://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=307538288';
location.href = url;

Categories

Resources