JavaScript窗口重定向不会启动

javascript window redirect doesn't fire up

本文关键字:启动 重定向 窗口 JavaScript      更新时间:2023-09-26

这个问题对我来说似乎很奇怪,我似乎无法解决它。 我确实有一个简单的 HTML 表单;在里面,我确实有一个文本框和一个按钮,如下所示:

<form id="form1" method="get"> <!-- Note: No Action property -->
    <div>
       <h1>
           Simple Test Form</h1>
       <table border="0" width="400">
           <tr>
               <td align="right">
                   All of these words
               </td>
               <td>
                   <input name="txtAll" id="txtAll" type="text" value="testing keyword" />
               </td>
           </tr>
           <tr>
               <td colspan="2" align="center">
                   <input type="submit" name="Submit" value="Submit" onclick="myJS(this);" />
               </td>
           </tr>
       </table>
   </div>
</form>

MyJS文件为:

<script type="text/javascript">
    function myJS(which) {
        var CONSTANT_SITE_TARGET = "http://servername/redirect/target.aspx?text=";
        //capture all values from the form
        var allWords = document.getElementById("txtAll").value;
        var morestring = ""; //More data manipulation here.....
        var url = CONSTANT_SITE_TARGET + allWords + morestring;
        window.open(url);
        //window.location = url;          //Doesn't work
        //window.location.href = url;     //Doesn't work
        //self.location = url;            //Doesn't work
        //top.location = url;             //Doesn't work
    }
</script>

如您所见,它不会重定向到 javascript 中的指定 URL。 当我使用window.open时,它可以工作。请注意,在<形式中...>标记中,我没有将 action 属性放入其中。 我不想打开新浏览器,只需重定向到同一浏览器中的新网址即可。

有没有办法重定向它?

不要使用表单标签。或者,将按钮的"type"属性设置为"按钮",而不是"提交"。当您单击该按钮时,表单会提交,但您不希望发生这种情况。删除表单或更改按钮都应修复不正确的重定向。当您不指定操作时,我很确定默认值是当前 URL。

好吧,只是一个想法。由于尚未设置 action 参数,因此submit按钮的默认行为是重新加载同一页面。您可以通过处理其onclick来改变该行为。也许存在可以通过在单击处理程序末尾使用 return false; 来解决的冲突,这会阻止该事件的默认操作。

这里

function myJS(which) {
    var CONSTANT_SITE_TARGET = "http://servername/redirect/target.aspx?text=";
    //capture all values from the form
    var allWords = which.txtAll.value;
    var morestring = ""; //More data manipulation here.....
    return CONSTANT_SITE_TARGET + allWords + morestring;
}

<form id="form1" method="get" onsubmit="this.action=myJs(this)">
<div>
    <h1>
        Simple Test Form</h1>
    <table border="0" width="400">
        <tr>
            <td align="right">
                All of these words
            </td>
            <td>
                <input name="txtAll" id="txtAll" type="text" value="testing keyword" />
            </td>
        </tr>
        <tr>
            <td colspan="2" align="center">
                <input type="submit" name="Submit" value="Submit" />
            </td>
        </tr>
    </table>
</div>
</form>

详细阐述评论:

<script>
window.onload=function() {
  document.getElementById("Submit").onclick=function() {
     var CONSTANT_SITE_TARGET = "http://servername/redirect/target.aspx?text=";
    //capture all values from the form
    var allWords = document.getElementById("txtAll".value;
    var morestring = ""; //More data manipulation here.....
    location = CONSTANT_SITE_TARGET + allWords + morestring;
  }
}
</script>
<div>
    <h1>
        Simple Test Form</h1>
    <table border="0" width="400">
        <tr>
            <td align="right">
                All of these words
            </td>
            <td>
                <input name="txtAll" id="txtAll" type="text" value="testing keyword" />
            </td>
        </tr>
        <tr>
            <td colspan="2" align="center">
                <input type="button" id="Submit" value="Submit" />
            </td>
        </tr>
    </table>
</div>

尝试以下重定向选项之一。我注释掉了其中的三个,以便代码保持有效,但你可以尝试任何一个,看看它是否适合你的需要。

    var CONSTANT_SITE_TARGET = "http://servername/redirect/target.aspx?text=";
    //capture all values from the form
    var allWords = document.getElementById("txtAll").value;
    var morestring = ""; //More data manipulation here.....
    var url = CONSTANT_SITE_TARGET + allWords + morestring;
     window.location.href= url;
     //window.navigate(url);
     //self.location(url);
     //top.location(url);