troubleshooting Question

how to call javascript function from codebehind to open a new window

Avatar of vedant0500
vedant0500 asked on
C#ASP.NET
7 Comments1 Solution761 ViewsLast Modified:

how to call javascript function from codebehind to open a new window but it should not be pop up window because most of the users keep pop up blocker on


i have used one the code snippet given by your site only but it does not work pls help


In your specific case, to call the javascript function that Daryal typed out, you would want a C# codebehind method that looks specifically like this:

protected void OpenWindow(string name, int age)
{
      StringBuilder sbJavaScript = new StringBuilder();
      sbJavaScript.AppendLine("<script type=\"javascript\">");
      sbJavaScript.AppendLine("OpenNewWindow(\"" + name + "\", " + age.ToString() + ");");
      sbJavaScript.AppendLine("</script>");
      ClientScript.RegisterStartupScript(Page.GetType(), string.Empty, sbJavaScript.ToString());
}

Just make sure that you have the below JavaScript function within your html markup:

<script type="text/javascript">
function OpenNewWindow(name,age)
{
window.open('http://xxx.com?name="+name+"&age="+age+")
}
</script>


pls help

thanks
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 7 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 7 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros