Tuesday, April 21, 2009

Javascript refresh page

If you want to use javascript to refresh the page, you can use
  • location.reload(); => post request, same as click the refresh button on the browser
  • window.location=window.location; => get request
  • self.location=self.location; => get request
If you want to use javascript to redirect to another page, you can use
  • location.replace('webform1.aspx'); => get request, does not create a history entry so browser back button disabled if no history before
  • location.href='webform1.aspx'; => get request, create a history entry so browser back button works
  • window.location='webform1.aspx'; => get request, create a history entry so browser back button works

IE buttons

  • Back button => get request
  • Forward button => post request
  • Refresh button => post request

If you get an IE error saying 'To display the webpage again, Internet Explorer needs to resend the information you've previously submitted', you can use window.location=window.location; to refresh the page instead of location.reload();

Sample code:

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="PostMethod_Test._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

<title></title>

</head>

<body>

<form id="form1" runat="server">

<div>

<asp:Literal ID="litJavascript" runat="server"></asp:Literal>

<div>

Post:

<div>

<asp:TextBox ID="txt" runat="server"></asp:TextBox>

Post Method:<asp:Button ID="btnPost" runat="server" Text="PostBack" />

</div>

</div>

<hr />

<div>

Redirect:

<div>

Get Method:<asp:Button ID="btnRedirect" runat="server" Text="ResponseRedirect" />

</div>

<div>

Get Method:<a href="javascript:location.replace('webform1.aspx');">JS Location Replace</a>

(does not create a history entry so browser back button disabled if no history before)

</div>

<div>

Get Method:<a href="javascript:location.href='webform1.aspx';">JS Location Href</a>

(create a history entry so browser back button works)

</div>

<div>

Get Method:<a href="javascript:window.location='webform1.aspx';">JS Window Location</a>

(create a history entry so browser back button works)

</div>

<div>

Get Method: IE Back button

</div>

<div>

Post Method: IE Forward button

</div>

</div>

<hr />

<div>

Refresh:

<div>

Get/Post Method:<a href="javascript:location.reload();">JS Location Reload</a> (if

there is any control in the form then after the form was submitted for the first

time, if you click this, it will be a post method coming with the IE alert)

</div>

<div>

Get Method:<a href="javascript:window.location=window.location;">JS Window Location</a>

(does not create a history entry) (if there is any control in the form then after

the form was submitted for the first time, if you click this, it will still be a

get method which means the form will not be submitted again)

</div>

<div>

Get Method:<a href="javascript:self.location=self.location;">JS Self Location</a>

(Same as above)

</div>

<div>

Get/Post Method: IE Refresh button - same as location.reload()

</div>

</div>

<hr />

<div>

Open New Window:

<div>

No Method:<a href="javascript:var a = window.open('webform1.aspx');">JS Window Open</a>

(just open)

</div>

<div>

Post Method for parent page:<asp:Button ID="btnOpen" Text="Open Window" runat="server" />

</div>

</div>

</div>

</form>

</body>

</html>


Partial Public Class _Default

Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

End Sub

Private Sub btnRedirect_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnRedirect.Click

Response.Redirect("Default.aspx")

End Sub

Private Sub btnPost_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPost.Click

End Sub

Private Sub btnOpen_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOpen.Click

Dim mstrMessage As New StringBuilder("")

mstrMessage.AppendFormat("<script type=""text/javascript"" >{0}", ControlChars.CrLf)

mstrMessage.AppendFormat(" window.open(""webform1.aspx"");{0}", ControlChars.CrLf)

mstrMessage.AppendFormat(" </script>")

litJavascript.Text = mstrMessage.ToString

End Sub

End Class

blog comments powered by Disqus