admin管理员组

文章数量:1287621

I have a weird issue with a "form" generated from js. The code is something like this:

var form_ = 
    '<form method="get" action="/foo/bar/" target="_blank">' +
        '<input type="hidden" name="a" value="' + a + '"/>' +
        '<input type="hidden" name="b" value="' + b +'"/>' +
        '<input type="hidden" name="c" value="' + c + '"/>' +
    '</form>';

$(form_).appendTo('body').submit().remove();

a is string variable that contains a date (dd/mm/yyyy) and b,c are numbers.

I use this snippet of code in a lot of places around the app, in fact there is a very similar code in the same HTML that opens a new tab as I expect, but this particular one tries to pop-up a window instead.

I've tried with Firefox, Chrome and IE.

I have a weird issue with a "form" generated from js. The code is something like this:

var form_ = 
    '<form method="get" action="/foo/bar/" target="_blank">' +
        '<input type="hidden" name="a" value="' + a + '"/>' +
        '<input type="hidden" name="b" value="' + b +'"/>' +
        '<input type="hidden" name="c" value="' + c + '"/>' +
    '</form>';

$(form_).appendTo('body').submit().remove();

a is string variable that contains a date (dd/mm/yyyy) and b,c are numbers.

I use this snippet of code in a lot of places around the app, in fact there is a very similar code in the same HTML that opens a new tab as I expect, but this particular one tries to pop-up a window instead.

I've tried with Firefox, Chrome and IE.

Share Improve this question edited Feb 21, 2012 at 9:25 user647772 asked Feb 20, 2012 at 11:05 codevinceDevcodevinceDev 411 silver badge4 bronze badges 1
  • Is there any reason you want it to submit and open the form in a new tab? – Aram Kocharyan Commented Feb 20, 2012 at 11:09
Add a ment  | 

3 Answers 3

Reset to default 8

target="_blank" is used to open targets in new windows. The fact that most modern browsers open new tabs instead is not defined by any standard and is usually controlled by user via settings.

If you have your browser set to open new links in a tab rather than in a window, and that doesn't work in this case you have probably found a bug.

Do the other places around your app post form or are they simply links?

The behaviour for opening new tabs or new instances of the browser are configurable for Internet Explorer 9 (Tools|InternetSettings|Tabs|Settings)

In Firefox, these options are in Tools|Options|tabs

Chrome doesn't seem to have a setting for this, but you can choose between new window or new tab, by right-clicking on a link.

From the answer given here: http://www.plus2net./html_tutorial/submit-new.php

<form name="f1" type="post" action="test5.html">
<input type="submit" value="Open default action file test5.html" onclick="this.form.target='_blank';return true;" />
</form>

EDIT: this works for me in Chrome, but I see no reason why this should generate any different behaviour going through JS or not. In the end the browser will decide what is to happen, and most likely both methods will end up with the same result.

本文标签: javascripttargetquotblankquot popups a window instead of open a new tabStack Overflow