0

I have a confirm dialog appearing on a form, which redirects the user to another page when clicked on ok.

However, is it possible to have that page open in a new window (e.g. target="_blank")

The code that I'm using is:

var answer=confirm("Do u want to continue?") {
if (answer==false) {
document.form.field1.focus();
return false;
}
window.location.href = "/some/url"
}
Donna Artis
  • 1
  • 1
  • 2

2 Answers2

0

So window.location refers to the location of your current window, so changing it of course takes you away from the window you're on. To open a new window, you want window.open - see Open a URL in a new tab (and not a new window) using JavaScript

However, I'd suggest instead, using a link that you intercept the default behavior for if the user answers false, and otherwise fallback to the default behavior.

ie:

<a href="/some/url" id="foo">bar</a>

$('#foo').click(function(e){
  var answer=confirm("Do u want to continue?") {
     if (answer==false) {
       document.form.field1.focus();
       e.preventDefault();
     }
  }
});

Although I'd have to double check if that actually works, I haven't worked with confirm much at all, so you might have to call the e.preventDefault() outside of that block.

Community
  • 1
  • 1
quoo
  • 6,237
  • 1
  • 19
  • 36
-2
window.open(
  '/some/url',
  '_blank'
);
Forte L.
  • 2,772
  • 16
  • 25