JavaScript窗口重定向不会启动
javascript window redirect doesn't fire up
这个问题对我来说似乎很奇怪,我似乎无法解决它。 我确实有一个简单的 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);
相关文章:
- 通过javascript重定向html传递php变量
- JavaScript下拉菜单-点击按钮并根据所选值重定向到url
- 使用angular重定向到html页面
- 如何在chrome扩展中重定向到html页面
- 无法从jquery Mobile导航栏重定向到另一个页面
- ng视图外的链接重定向到ng视图内的页面
- 在phonegap应用程序内部重定向不起作用
- CORS-重定向到第二个GET正在接收的页面
- 使用重定向来启动应用程序,并且仍然停留在asp.net中的同一页面中
- JavaScript窗口重定向不会启动
- JS在第一次访问时重定向到启动页面
- CasperJS的启动在应该有重定向时不起作用
- 如何通过单击取消javascript函数启动的定时重定向
- 快速重新启动匹配路由-重定向
- 使用二维码启动应用程序或重定向到网页
- 启动屏幕后,iPhone ChildBrowser重定向
- 从webview启动应用程序,如果未安装,则重定向到播放商店
- javascript:启动新窗口,重定向现有窗口,在新窗口中设置焦点-如何
- 移动电子邮件启动+点击事件重定向
- 是否可以重定向用户以启动扫描仪应用程序