franglais
franglais

Reputation: 938

Javascript link not opening in new window

So I have the code below that is working on all users bar one. On that user's machine it's opening in the window where the link is instead of opening a new window. This is causing issues which I won't go into here!

Javascript is enabled on the user's PC as far as I can tell (ie options -> security -> custom level -> enable active scripting) so any other ideas?

<form action="http://www.website.com" method="post" onsubmit="target_popup(this)">
<input name="userid" type="hidden" value="[userid]" />
<input type="submit" value="New Window" />
</form>
<script type="text/javascript">
function target_popup(form) {
    window.open('', 'formpopup', 'width=1100,height=750,resizable,scrollbars');
    form.target = 'formpopup';
}
</script>

It's Windows 7. The browser is a built in one on an application, but I think it's basically IE9 and the popup should be popping up in a proper IE9 window (as opposed to opening in the application's browser which is why it needs to be in a new window)

She's also got Chrome installed on the PC (but so have other users who don't have the same issue)

Upvotes: 0

Views: 61

Answers (2)

mplungjan
mplungjan

Reputation: 177830

You need to return false on the submit or the form will change page before your popup is activated.

Since some browsers take a bit to figure out a window was opened, you need to use setTimeout and also test for popup blockers

I cannot imagine popping in a new window will open IE9 from chrome unless the protocol is one only IE9 is set to recognise.

That said, try

<form id="form1" action="http://www.website.com" method="post" onsubmit="return target_popup(this)">
<input name="userid" type="hidden" value="[userid]" />
<input type="submit" value="New Window" />
</form>
<script type="text/javascript">
function target_popup(form) {
  form.target = 'formpopup';
  var w = window.open('', 'formpopup', 'width=1100,height=750,resizable,scrollbars');
  if (w) { 
    setTimeout(function() { document.getElementById("form1").submit()},100);
    return false;
  }
  return true; // submit anyway since a popup blocker was active
}
</script>

Upvotes: 1

T.J. Crowder
T.J. Crowder

Reputation: 1074218

I believe once the submit event is raised, the form target has already been selected.

If you trigger your function prior to the submit event being raised — for instance, when a submit button is pressed or similar — then when the form is submitted you should see the new target get used.

Upvotes: 0

Related Questions