Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling Javascript function from Code behind page

I have a script that I want to pop a window after 5 page views. The java script works fine on the default.aspx page with a link to call it. But I want to launce it from my default.aspx.cs page after my session var count gets to 5. How can I do this? Is it possible?

default.aspx

  <script type="text/javascript">
    window.name = "Register";
    function popWin(link) {
        var w = window.open(link.href, link.target, 'width=500,height=600,resizable');
        return w ? false : true; // if popup blocker, use the default behaviour of the link 
    } 
</script>

Default.aspx.cs page

 if (Session["PagesViewed"].ToString() == "5")
            {
              //Call my Javascript function How?????

            }
like image 316
CsharpBeginner Avatar asked Sep 25 '26 04:09

CsharpBeginner


1 Answers

You can output javascript into a LiteralControl from your code behind:

.aspx:

<asp:Literal id="myLiteral" runat="server" />

Code behind:

myLiteral.Text = "<script type='text/javascript'>popWin('url');</script>";

When rendered this way, the output script will call the function - make sure it is lower in the page than where the function was defined to ensure it exists.

like image 190
Oded Avatar answered Sep 26 '26 16:09

Oded



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!