we need a kind of client-side redirection and the only options we have are:
window.locationwindow.open- HTTP 301, 302, 303 responses
window.location doesn’t support opening the target location in a new tab or window. (I think this is related to browsing context)
window.open doesn’t work in Chrome and seems to be dependent upon some client-side configurations made to the browser, which makes it a less favorable option, since we don’t have control on it.
HTTP redirect response doesn’t open a new tab or window, just like window.location.
Any idea?
After playing around with this for hours last night, I had a flash of inspiration this morning, and I have just tested this solution in FF3, Chrome, IE7 and IE8 – it’s a bit hacky but it works in all.
How It Works
You need a hidden
<div>on your page somewhere that you can get a reference to in JS (no big deal – just create one withdisplay: none).When you want to do the redirect, use
document.createElement()to create a new<form>element, assign it’sactionattribute with the address of the page where you want to take the user, and give it atargetattribute as appropriate (I have used_blankin my example above). Then simply append it to your hidden<div>and call it’ssubmit()method.Hey presto, your redirect opens in a new window/tab!
Unfortunately you are still at the mercy of how the user configures their browser to handle
target="_blank"but it will be either a new window or a new tab, and it should work everywhere…