admin管理员组文章数量:1278984
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:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Page1.aspx.cs" Inherits="WebApplication1.Page1" %>
<!DOCTYPE html>
<html xmlns="">
<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'
.
I have tried using window.location.search but still without success.
What I'm doing wrong?
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:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Page1.aspx.cs" Inherits="WebApplication1.Page1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3/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'
.
I have tried using window.location.search but still without success.
What I'm doing wrong?
Share Improve this question edited Jun 10, 2017 at 18:57 Kappacake 1,9471 gold badge22 silver badges45 bronze badges asked Dec 31, 2014 at 18:46 EduardoEduardo 7093 gold badges9 silver badges21 bronze badges 6-
replace
onclick="Redirect();"
withonclick="Redirect()"
. – user3310334 Commented Dec 31, 2014 at 18:47 -
6
As you don't prevent the default behavior of the event I would guess (without being able to test right now) that the redirect triggered by the
submit
action overwrites the action of the script. – t.niese Commented Dec 31, 2014 at 18:49 - @theonlyguti, doesn't work. – Eduardo Commented Dec 31, 2014 at 18:52
-
3
I think t.niese is right; try
onclick="Redirect(); return false;"
to squash the default submit. – sifriday Commented Dec 31, 2014 at 18:53 - @t.niese works perfectly! – Eduardo Commented Dec 31, 2014 at 18:54
5 Answers
Reset to default 3Use document.location instead.
document.location = 'http://localhost:61267/Page1.aspx?q=name';
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
<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.
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/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.';
}
</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/TR/html4/strict.dtd">
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
Only changed the input type, and it worked perfectly! Thanks to t.niese.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Page1.aspx.cs" Inherits="WebApplication1.Page1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3/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>
you need to do either of the following:
1) For server side asp code:
<asp:button text="Navigate" onclientclick="Redirect()" value="pesq" runat="server" />
See MSDN Example
2) if client side code:
<input type="button" value="pesq" onclick="Redirect();"/>
本文标签: javascriptRedirect using windowlocation doesn39t workStack Overflow
版权声明:本文标题:javascript - Redirect using window.location doesn't work - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741256824a2366839.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论