最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - How can I change the target of a form from javascript? - Stack Overflow

programmeradmin5浏览0评论

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
Add a ment  | 

3 Answers 3

Reset to default 3

I 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 on Submit, the target will be '_self', as the function changeFormTarget is being called, and form will submit in the same tab.
  • If you click on Submit without clicking on Click 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>
发布评论

评论列表(0)

  1. 暂无评论