Redirect using window.location doesn't work

12,854

Solution 1

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">

Solution 2

Use document.location instead.

      document.location = 'http://localhost:61267/Page1.aspx?q=name';  

Solution 3

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.

Share:
12,854
Eduardo
Author by

Eduardo

Updated on August 13, 2022

Comments

  • Eduardo
    Eduardo over 1 year

    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.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'.

    I have tried using window.location.search but still without success.

    What I'm doing wrong?

  • Eduardo
    Eduardo over 9 years
    Like Riad, it doesn't work for me, but thanks anyway.