views:

103

answers:

2

I have JSP page with method = POST and action='/mydir/mypage/nextpage'

I have a button:

<button title='Continue' onclick="this.form.perform.value='cancelButton'; javascript:doCloseWindow();">Continue</button>

and JavaScript method like:

function doCloseWindow(){         
  location.href = "https://abc.xyz.com/mydir/?param=123";              
}

It does not work in Firefox 3.6. On click of button; it redirects to the path I mentioned in form action.

With Tamper data I find that the request goes to URL (as in method) with GET and then it redirects to form's action URL.

I added return false in the method call also -- javascript:doCloseWindow();return false

I tired various combinations like

window.location.href = "https://abc.xyz.com/mydir/?param=123";
window.document.location.href = "https://abc.xyz.com/mydir/?param=123";
document.location.href = "https://abc.xyz.com/mydir/?param=123";

But no success.

+1  A: 

Remove the "javascript:" before the call to doCloseWindow.

At this point, you've already executed some JavaScript code in this event handler — it doesn't make sense to try to tell the browser again that the following code is JavaScript.

Syntactic
Removed, still not working
Anurag
"Not working" with the same symptom? In your comment to Dean J's answer, you mention that "it is going to that URL but status is returned as 302 and then goes to some other default URL".If your code is sending the browser to the right URL, but the server is redirecting it, then your code is fine; either the URL you're using is wrong or the server is doing something you didn't expect.
Syntactic
I just tried removing the javascript as you said and using 'return false' after the method call. Also I removed the part 'this.form.perform.value='cancelButton'' and then only it works.I tried to set the above value in my method, again it submits the form and not goes to new URL.I do not understand, whenever I try to do some other operation before calling the location.href , it always submit the form.
Anurag
A: 

Try changing your Javascript call to:

function doCloseWindow(){
    alert('here');
    location.href = "https://abc.xyz.com/mydir/?param=123";
    return false;
}

I'm wondering if the function is actually running.

Dean J
It comes to the function. also I can see teh URL in Tamper data tool. But it is not getting re-directed to that URL.I am using <input type=button> in place of <button>. Now it is going to that URL but status is returned as 302 and then goes to some other default URL.
Anurag
Thanks a lot guys ... it works after combining your suggestions and removing the this.form.perform ... part. But do not understand why it submits the form if I try to do something before calling location.href
Anurag