How to Open Form Submit in New Window?

by 3 replies
4
Hi,

I am currently using a form where there are 3 selections with radio buttons next to them. Depending on the radio button a person selects, when they submit the form they are then taken to a corresponding url. So there are 3 different urls they can be taken to depending on the selection they make before submitting the form.

The problem I am having is getting those urls they are taken to upon submitting the form, to open up in a NEW browser window. At the moment they are just forwarded to that url in the same window but that's not what I want.

Any ideas how to make this work. Here is the current code I am using.

Note: I have already tried adding the target="_blank" to the form action line but that does not work.

Code:
<script type="text/javascript">

  function doSubmit(form) {
    var urls = form['url'];
    var i = urls && urls.length;
    while (i--) {
      if (urls[i].checked) {
        window.location = urls[i].value;
      }
    }
    return false;
  }

</script>
</body>
Code:
<form action="#" onsubmit="return doSubmit(this)">
<input type="radio" name="url" class="styled" checked="checked" value="http://google.com/option1" /><br />
<input type="radio" name="url" class="styled" value="http://google.com/option2" /><br />
<input type="radio" name="url" class="styled" value="http://google.com/option3" /><br />
<input name="" type="submit" class="button2" value="Select" />
</form>
#programming #form #open #submit #window
  • Banned
    [DELETED]
  • Dont do it the way you are. Try it this way.

    add target="_blank" to form

    Then use JQUERY to change the forms action attr as needed

    .attr() – jQuery API
  • Instead of window.location, use window.open().
    window.open(urls[i].value);
    • [1] reply
    • There are only 2 ways to do this, and neither method is 100% reliable. Some browsers ignore target="_blank" entirely, and the Javascript method (window.open or window.location) is blocked by a lot of popup blockers, firewalls, etc. Not to mention it won't work at all for anyone who has Javascript disabled.

      So the best you can do is use the Javascript method first, then for anyone who has JS disabled, put your target="blank" inside a <noscript> tag as a backup. Even if you do that, it will not work for every single visitor due to the reasons mentioned above, but it's better than just relying on a single method.

Next Topics on Trending Feed

  • 4

    Hi, I am currently using a form where there are 3 selections with radio buttons next to them. Depending on the radio button a person selects, when they submit the form they are then taken to a corresponding url. So there are 3 different urls they can be taken to depending on the selection they make before submitting the form.