简体   繁体   中英

Redirecting to a new URL using javascript

I am trying to redirect my page to a different url depending on what the user selects from the drop down list. If the user selects "A", the url is the default URL that's in the action attribute of the form but if the user selects "B", I want to redirect it to a different url.

My problem has been no matter what I do, it always redirects to the default URL(ie oldUrl.do) even if I select "B" from the drop down.

What can I do to point it to the newUrl.do?

Note: I am using Struts 1.2 and testing it on IE8.

My jsp page:

<form name="myForm" method="post" action="oldUrl.do" id="myForm">

    <select name="modeOfT" id="choice"><option value="A">A</option>
        <option value="A">A</option>
        <option value="B">B</option>
    </select> 

    <input type="submit" name="submitBtn" onclick="submitForm()">
</form>

Javascript function:

function submitForm(){
    var form = document.getElementById("myForm");
    var mode = document.getElementById("choice");
    if(mode.value == "B"){
        $(form).attr("action", "newUrl.do");
    }
} 

Following are other things I have tried and they still take me to "oldUrl.do":

  1. document.location.href("newUrl.do");

  2. window.location.href="newUrl.do";

  3. top.location.href="newUrl.do";

  4. parent.location.href="newUrl.do";

  5. window.location.replace("newUrl.do");

  6. window.location="newUrl.do";

You should not handle the click event. To deal with a form being submitted you should use the submit event, for this and many other reasons (people can use enter, they can hit space while focus in on the submit button)

Working example http://jsfiddle.net/xv3Uf/2/

HTML

<form name="myForm" method="post" action="oldUrl.do" id="myForm" onsubmit="return submitForm()" >

JS

function submitForm(){
    var form = document.getElementById("myForm");
    var mode = document.getElementById("choice");
    if(mode.value == "B"){
        form.action = "newUrl.do";
    }
} 

And since you're already using jQuery , why not drink the kool-aid and stop the pre-enlightenment practice of setting JS handlers in the HTML? http://jsfiddle.net/xv3Uf/3/

<form name="myForm" method="post" action="oldUrl.do" id="myForm">

JS

$('#myForm').submit(function(e){
    if ( $('#choice').val() === 'B') {
        this.action = 'newUrl.do'
    }
});

Turn the <submit> into a regular <button> .

In the submitForm() function, at the end, call:

document.forms["myForm"].submit();

You could:

  1. Have the server send back an HTTP/redirect header depending on what the user selects in the form.

  2. If you want the redirect to be handled at the browser, use an "onsubmit" attribute, and return a false value. You can handle the redirection within this function using window.location = <URL>;

     <form onsubmit="return myJSFunc()" .. > ... <input type="submit"> </form> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM