簡體   English   中英

從Code后面的頁面調用Javascript函數

[英]Calling Javascript function from Code behind page

我有一個腳本,我想在5頁瀏覽后彈出一個窗口。 java腳本在default.aspx頁面上工作正常,並帶有一個調用它的鏈接。 但是我想在會話var count到達5之后從default.aspx.cs頁面中將它拉出來。我該怎么做? 可能嗎?

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頁面

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

            }

您可以從后面的代碼LiteralControl javascript輸出到LiteralControl

的.aspx:

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

代碼背后:

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

以這種方式呈現時,輸出腳本將調用該函數 - 確保它在頁面中比定義函數的位置低,以確保它存在。

在ASP.Net中,您可以執行以下操作:

Page.ClientScript.RegisterStartupScript(
    this.GetType(),
    "openpopup",
    "popWin('www.someurl.com');",
    True);

如果您需要更多地控制腳本放置@Oded有更好的方法 - 因為嘗試調用尚未定義的函數不是一個好主意...

你不能直接從C#調用javascript函數。 但是,您可以做的是將<script>傳遞給執行該功能的瀏覽器。

response.Write("<script>popWin(something);</script>");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM