Peter
Peter

Reputation: 211

Open page in new tab/window AND direct current window to a new url

I have set my form action to "_blank" so that the pdf document I am creating opens in a new tab/window, this works fine but I want the source page to simultaneously redirect to a different URL. How can this be done? HTML and/or JavaScript solutions please. Thanks in advance.

Upvotes: 1

Views: 1452

Answers (2)

fdomn-m
fdomn-m

Reputation: 28621

Add an onsubmit to the form and change location.href. Worked fine in IE10, but failed in Chrome.

<form id="form1" onsubmit='formonsubmit();' method="get" action="http://google.com" target="_new">
    <input type="submit" value="submit" />
</form>                                   

<script type="text/javascript">
    function formonsubmit() {
        location.href = "http://stackoverflow.com";
    }
</script>

For the purists, add the onsubmit via a handler, works in both IE and GC:

<form id="form1" method="get" action="http://google.com" target="_new">
    <input type="submit" value="submit" />
</form>                                   

<script type="text/javascript">
    $(function() {
        $("#form1").on("submit", function () {
            location.href = "http://stackoverflow.com";
        });
    });
</script>

Upvotes: 2

Thriggle
Thriggle

Reputation: 7059

This depends a bit on how your HTML is formatted and how you're generating the URL in the first place.

It is possible to attach an onclick event to an anchor tag that does something in the current page (such as redirect the page to a new URL) in addition to whatever navigation action the anchor tag produces.

document.querySelector("a").onclick = function() {
  window.location.href = "http://www.jsfiddle.net";
};
<a href="http://www.bing.com" target="_blank">Open Bing in new window and navigate to jsFiddle.net</a>

Upvotes: 2

Related Questions