javascript 使用 window.location 重定向不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27725127/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Redirect using window.location doesn't work
提问by Eduardo
I searched for a related post, but all attempts have not worked.
我搜索了一个相关的帖子,但所有的尝试都没有奏效。
My problem, is that I'm trying to do a simple redirect in Javascript, using path+querystring. My code is below:
我的问题是,我正在尝试使用路径+查询字符串在 Javascript 中进行简单的重定向。我的代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Page1.aspx.cs" Inherits="WebApplication1.Page1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="submit" value="pesq" onclick="Redirect();"/>
</div>
</form>
<script>
function Redirect()
{
window.location.href = 'http://localhost:61267/Page1.aspx?q=name';
}
</script>
</body>
</html>
window.location.href is always set like 'http://localhost:61267/Page1.aspx'
.
window.location.href 始终设置为'http://localhost:61267/Page1.aspx'
.
I have tried using window.location.search but still without success.
我曾尝试使用 window.location.search 但仍然没有成功。
What I'm doing wrong?
我做错了什么?
采纳答案by Logic1
Not sure why you need a form and submit button for just doing redirects.
不知道为什么你需要一个表单和提交按钮来进行重定向。
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Page1.aspx.cs" Inherits="WebApplication1.Page1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<div>
<input type="button" value="pesq" onclick="Redirect();"/>
</div>
<script>
function Redirect()
{
window.location.href = 'http://www.google.com';
}
</script>
</body>
</html>
also i noticed some encoding issues that i was able to resolve by adding:
我还注意到一些编码问题,我可以通过添加以下内容来解决这些问题:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
回答by Joe Thomas
Use document.location instead.
请改用 document.location。
document.location = 'http://localhost:61267/Page1.aspx?q=name';
回答by Ryan
Problem code:
问题代码:
<form id="form1" runat="server">
<div>
<input type="submit" value="pesq" onclick="Redirect();"/>
</div>
</form>
HTML elements, specifically, forms, have default events that occur when user actions take place. In your context, the submit
event is probably being invoked when you click on that input
box. You need to prevent the default event from happening by either returning false from the event handler, or calling preventDefault
HTML 元素,特别是表单,具有在用户操作发生时发生的默认事件。在您的上下文中,submit
当您单击该input
框时可能会调用该事件。您需要通过从事件处理程序返回 false 或调用来防止默认事件发生preventDefault
<form onsubmit="Redirect(); return false;" id="form1" runat="server">
<div>
<input type="submit" value="pesq" onclick="Redirect(); return false;"/>
</div>
</form>
P.S.
聚苯乙烯
The onsubmit
handler will allow yourself to press the enter key and still have the same effect as would have happened if you just clicked the input
box.
该onsubmit
处理器将让自己按回车键,仍然有同样的效果都不会发生,如果你只是点击input
复选框。
回答by Riad
you need to do either of the following:
您需要执行以下任一操作:
1) For server side asp code:
1)对于服务器端的asp代码:
<asp:button text="Navigate" onclientclick="Redirect()" value="pesq" runat="server" />
2) if client side code:
2)如果客户端代码:
<input type="button" value="pesq" onclick="Redirect();"/>
回答by Eduardo
Only changed the input type, and it worked perfectly! Thanks to t.niese.
只更改了输入类型,并且完美运行!感谢 t.niese。
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Page1.aspx.cs" Inherits="WebApplication1.Page1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="button" value="pesq" onclick="Redirect();return false;"/>
</div>
</form>
<script>
function Redirect()
{
window.location.href = 'http://localhost:61267/Page1.aspx?q=name';
}
</script>
</body>
</html>