2

I've been working on this for about 2 hours now and can't seem to find a solution. I know you can just do this:

<form target="_blank"

That only works when the user submits it. The thing is, this form is submitted using javascript. It's submitted automatically. So when I do document.formname.submit(); and the form has the attrivute target="_blank" it gives me the following error:

Error: uncaught exception: [Exception... "Component returned failure code: 0x80004005 (ns_error_failure) [nsidomhtmlformelement.submit]" nsresult: "0x80004005 (ns_error_failure)" location: "JS frame :: [lazyjhunting.com...] :: <TOP_LEVEL> :: line 95" data: no]

So doesn't anyone know how to automatically submit a form and have the results open in a new window?

Any help is greatly appreciated. Thanks a lot.

Adeel
  • 605
  • 1
  • 6
  • 16

3 Answers3

1

it can be done.. Please check below code:

    window1.jsp
    -----------------

        <script type="text/javascript">
        function formSubmit(){
            document.forms['myform'].submit();
        }
        </script>

        <form action="window2.jsp" method="get" name="myform">
        <input type="text" name="name">
        <input type="button" name="clickMe" value="clickMe" onclick="formSubmit();">
        </form>



        window2.jsp
        -----------------
        <body>
        <%= (String)request.getParameter("name")%>
        </body>

you can execute document.forms['myform'].submit() in any way, I just used the simple button to call the function formSubmit to execute document.forms['myform'].submit()

bragboy
  • 34,892
  • 30
  • 114
  • 171
dku.rajkumar
  • 18,414
  • 7
  • 41
  • 58
  • Thanks for your response, well I want to submit form automatically in a new window. – Adeel Nov 23 '11 at 08:58
  • sorry I am not getting you exactly. what do you mean by "submit form automatically in a new window" – dku.rajkumar Nov 23 '11 at 09:27
  • Means for example on body onload event. I want to submit it without user interaction. – Adeel Nov 23 '11 at 10:31
  • My form is in window1, when I'll submit my form by document.form.submit() then it should be submitted on new window means a new window will be opened and all posted values will be displayed on it. got it?? – Adeel Nov 23 '11 at 10:56
  • http://stackoverflow.com/questions/178964/javascript-post-on-form-submit-open-a-new-window – dku.rajkumar Nov 23 '11 at 11:21
1

I think you can't do it because opening new window without user interaction will be blocked by browser's popup blocker similar to how popups are blocked. You have to bind it to click event for example.

Johny
  • 1,441
  • 10
  • 26
0

You can certainly do it. Here's the basic code

<html>
<head>
</head>
<body onload="document.f.submit();">
<form target="_blank" id='f' name='f' method='POST' action="http://www.yahoo.com">

</form>

<input type='button' name='btn' value='Submit' onclick='document.f.submit();' />
</body>
</html>

The question is (and maybe this was just a bad example) why would you want so submit a form on load event? If the page just got loaded, user hasn't had a chance to enter anything yet. So, what user input is getting posted. If it's something you loaded from the database, why are you posting it right away instead of performing whatever you needed on the server before you served the original page.

If you're posting data to another window that was just loaded from db, why not open a separate window and load that data from the database directly? Maybe I just missed something.

Alexey Gerasimov
  • 2,131
  • 13
  • 17