Eduardo
Eduardo

Reputation: 709

Redirect using window.location doesn't work

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?

Upvotes: 2

Views: 13300

Answers (5)

Ryan
Ryan

Reputation: 14649

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.

Upvotes: 3

Riad
Riad

Reputation: 3850

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();"/> 

Upvotes: 0

Logic1
Logic1

Reputation: 1847

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

Upvotes: 1

Eduardo
Eduardo

Reputation: 709

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

Upvotes: 0

Joe Thomas
Joe Thomas

Reputation: 6397

Use document.location instead.

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

Upvotes: 3

Related Questions