jijo
jijo

Reputation: 815

Issue with javascript href

I have a jsp page in struts 1.2 which has 2 functionalities. Print pdf and submit form.
Print pdf will open a new window and display a pdf generated with the field values in the page.
Submit form should navigate to confirmation page in the same window and save values in db.
Here are the part of code for it.

<a href="javascript:void(0)" onclick="javascript:printPDF();" target="_blank"><img src="/images/buttons/ButtonPrint.gif" border="0" alt="Print" ></a>

<a href="javascript:void(0)" onclick="javascript:submitContinue();"><img src="/images/buttons/ButtonSubmit.gif" border="0" alt="Submit" ></a>


Here are the corresponding javascript methods.

function submitContinue()
{
    document.forms[0].action = "/provider/requestSubmitted.do?method=processUpload";
    document.forms[0].submit();
    return true;
}

function printPDF()
{
    document.forms[0].action = "/provider/requestSubmitted.do?method=printPDF";
    document.forms[0].target="new";
    document.forms[0].submit();
    return true;

}

These functionalities works fine separately. But if I click on print and then on submit, the next page(confirmation page) opens in a new tab. It should be in the same window. Please help !!

Upvotes: 1

Views: 563

Answers (2)

jrey
jrey

Reputation: 2183

Jijo,

you have a script to submit a form, in your form object to submit has a target attribute, when the target has value new or xxx (tab name) this works fine, you must remove the target value from your form object.

 function submitContinue()  
 {
    document.forms[0].action = "/provider/requestSubmitted.do?method=processUpload";
    document.forms[0].target="_self";
    document.forms[0].submit();
    return true;
}  
 function printPDF()
 {
     document.forms[0].action = "/provider/requestSubmitted.do?method=printPDF";
     document.forms[0].target="pdf";
     document.forms[0].submit();
     return true; 
 }

if you have

 <form action="xxx" **target="yyy"**

you must remove again target="yyy"

The documentation about target attribute is

target values      Description
  _blank            Specifies where to display the response that is 
  _self
  _parent
  _top

Upvotes: 2

Michele Mariotti
Michele Mariotti

Reputation: 7469

function submitContinue()
{
    document.forms[0].action = "/provider/requestSubmitted.do?method=processUpload";
    document.forms[0].target=self; <-------HERE
    document.forms[0].submit();
    return true;
}

when you use printPDF the form target is set to "new", and if you don't refresh the page or reset it to "self" (or null) that form will open a new window on every submit.

Upvotes: 1

Related Questions