mrdudu
mrdudu

Reputation: 23

Run JavaScript before redirect to other page

I try to open a popup window with JavaScript before redirecting to another page.

I've written this code:

ScriptManager.RegisterStartupScript(this.Page, typeof(Page), "abc", "window.open('http://yahoo.com','2','resizable=yes,directories= no,menubar=no,top=null,left=00, width=100,height=100')", true);
Response.Redirect(p.Url);

but it is only a redirect and a popup does not open.

How do I open a popup window before the user user redirects to another page?

Upvotes: 2

Views: 6531

Answers (2)

Rajendra
Rajendra

Reputation: 46

If you are doing anything like opening popup or window...before response.redirect(); , it will not work. If you want to redirect to any page after opening pop up boxes, please ensure that redirection should be written in Javascript only not in C#

This will not work

Response.Write("<script language='javascript'>alert('hello')</script>");  
Response.Redirect("About.aspx"); <br>

This will work

Response.Write("<script language='javascript'>alert('hello');
window.location.href='About.aspx';</script>");

OR

if (!ClientScript.IsStartupScriptRegistered("alert"))
{ 
Page.ClientScript.RegisterStartupScript(this.GetType(), "alert",     "alertMe();window.location.href='About.aspx';", true);
}

In this way we can call External JS files also

 Page.ClientScript.RegisterStartupScript(this.GetType(),    "callJS", 
 "&alt; script language='javascript' src='sample.js' &glt;  ");

Upvotes: 3

Mehmet
Mehmet

Reputation: 1874

If you are using ScriptManager.RegisterStartupScript you should insert your javascipt code to top of your aspx page(in <head> tags)

A working example for me;

ScriptManager.RegisterStartupScript(this.Page, typeof(Page), "abc", "<script>OpenWin();</script>", false);
Response.Redirect(p.Url);

In aspx head tags;

<script type="text/javascript">

 function OpenWin() {
        window.open('http://yahoo.com', '2', 'resizable=yes,directories= no,menubar=no,top=null,left=00, width=100,height=100');
   }

</script>

If this code not works, another way; You can use javascript for redirect. Like this ;

window.location.href="p.Url";

But on this way you should get p.Url value to javascript. You can do this a lot of way, (hiddenfield, session etc)

Upvotes: 0

Related Questions