2

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 -

  1. Determine the prior/previous/last page the user came from, store it as a variable
  2. Open a new window or tab, to a specified URL
  3. window.location back to the prior page, which I stored as a variable

Hope that makes sense.

RCNeil
  • 8,581
  • 12
  • 43
  • 61
  • `window.open` might be what you are looking for. It's not new tab but might be what you are looking for. – clentfort Nov 15 '12 at 15:51
  • before you spend much time troubleshooting with window.open, remember that it can only be executed from a user action such as a mouse click. – Salketer Nov 15 '12 at 15:54
  • so, in that regard, can I have `window.open = "http://www.example.com"; window.location = priorpage;` and then use javascript to determine what `var priorpage` would be? – RCNeil Nov 15 '12 at 15:54
  • ooops.... good call then @Salketer – RCNeil Nov 15 '12 at 15:55
  • see [this question on SO](http://stackoverflow.com/questions/4907843/open-url-in-new-tab-using-javascript) – didierc Nov 15 '12 at 15:55
  • also, window.open is pretty much a pop-blocking haven... – shennan Nov 15 '12 at 15:57
  • I don't get what you are trying to do... You want to click a link that goes to a page with JS that opens a window and then goes back to previous page? – Salketer Nov 15 '12 at 15:58
  • No clicks. Above shows an instantenous redirect, because I cannot touch the `header` of the page. I want to open a new window/tab of a specified URL, then redirect the current window/tab to it's previous page. – RCNeil Nov 15 '12 at 16:00

1 Answers1

3

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

Sean Airey
  • 6,352
  • 1
  • 20
  • 38
  • Ok. Opening a new tab or window are both acceptable in this case. – RCNeil Nov 15 '12 at 16:21
  • Ok I've updated my answer =] window.open might get blocked by the user's popup blocker, however SharePoint should already be in the local intranet zone or trusted sites so you should be alright. – Sean Airey Nov 15 '12 at 16:24
  • Thanks. Is there a way to retrieve the last page the visitor was on before this page? I could store that as `mySavedUrl`, for the purpose of returning them to the page they were before. – RCNeil Nov 15 '12 at 16:26
  • I've also added in some info about document.referrer as using history.back can send you to a page that needs form re-submission which could confuse users or do things you don't want by having them re-post form data. – Sean Airey Nov 15 '12 at 16:31