So I am trying to change the target of a form in order to open a new window. Any kind of redirection of this form use the same window but one. The latter is a button that calls a custom javascript function that must change the target from "_self", set by default, to "_blank" that apparently does the job I'm wanting.
This is my form tag
<html:form action="<%=nomeAction%>" target="_self">
...
<html:button styleClass="button" property="button"
onclick="javascript:redirectOnAnewWindow('mostraConguagli770')">
<bean:message key="button.mostraConguagli770"/>
</html:button>
</form>
So this is the js function
function redirectOnAnewWindow(event) {
document.forms[0].action=document.forms[0].action+"?event="+evento;
//document.forms[0].setAttribute("target", "_blank");
document.forms[0].target = "_blank";
document.forms[0].submit();
}
I've tried both the mented one and the non mented one, but none of them worked. So I'm not sure what's the problem but changing manually the target of the form, and fixing the value to "_blank" works perfectly fine, the only problem is that I need it to change the target only when that one button is pressed.
Is there any possible way to do this? Because I looked it up but couldn't find anything that solved my issue
So I am trying to change the target of a form in order to open a new window. Any kind of redirection of this form use the same window but one. The latter is a button that calls a custom javascript function that must change the target from "_self", set by default, to "_blank" that apparently does the job I'm wanting.
This is my form tag
<html:form action="<%=nomeAction%>" target="_self">
...
<html:button styleClass="button" property="button"
onclick="javascript:redirectOnAnewWindow('mostraConguagli770')">
<bean:message key="button.mostraConguagli770"/>
</html:button>
</form>
So this is the js function
function redirectOnAnewWindow(event) {
document.forms[0].action=document.forms[0].action+"?event="+evento;
//document.forms[0].setAttribute("target", "_blank");
document.forms[0].target = "_blank";
document.forms[0].submit();
}
I've tried both the mented one and the non mented one, but none of them worked. So I'm not sure what's the problem but changing manually the target of the form, and fixing the value to "_blank" works perfectly fine, the only problem is that I need it to change the target only when that one button is pressed.
Is there any possible way to do this? Because I looked it up but couldn't find anything that solved my issue
Share Improve this question asked May 8, 2020 at 15:46 L_CleoL_Cleo 1,5571 gold badge15 silver badges35 bronze badges 1- Most browsers let the user control the opening of new windows and tabs through their settings. – collapsar Commented May 8, 2020 at 15:53
3 Answers
Reset to default 3I think your problem e form your html in tag, because this work well without.
see :
I made a jsfiddle : https://jsfiddle/4puxr6e3/
html
<form action="blabla.html" target="_self">
<button class="button" onclick="javascript:redirectOnAnewWindow('mostraConguagli770')">
Redirect
</button>
</form>
javascript
function redirectOnAnewWindow(event) {
document.forms[0].action="blibli.html";
document.forms[0].target = "_blank";
document.forms[0].submit();
}
I resolve this requirement using JavaScript with this code:
<form onsubmit="this.action=this.target.value;" action=""><div>
<fieldset>target: <select name="target" class="fwb pad05 tc">
<option value="https://web1.tld/cart.php">https://web1.tld/cart.php</option>
<option value="https://web2.tld">https://web2.tld</option>
<option value="https://www.cia.gov/documents/">https://www.cia.gov/documents/</option>
<option value="https://www.dea.gov/login4friends/">https://www.dea.gov/login4friends/</option>
</select><p class="m03 td">id <label><input type="text" name="...
... rest of form
note all the "trick" is on:
onsubmit="this.action=this.target.value;"
The below html code works well. You can refer it and change your code accordingly.
- If you click on
Click Me
and then click onSubmit
, the target will be'_self'
, as the functionchangeFormTarget
is being called, and form will submit in the same tab. - If you click on
Submit
without clicking onClick Me
, the target will be'_blank'
and form will submit in a new tab.
<html>
<form action="/somaction" target="_blank">
<button onclick="changeFormTarget(this)" type="button"> Click Me </button>
<button type="submit">Submit</button>
</form>
<script>
function changeFormTarget(el) {
console.log(el.form.target)
el.form.setAttribute('target', '_self')
console.log(el.form.target)
}
</script>
</html>