从后面的代码调用jQuery函数

问题描述:

如何从页面后面的代码中调用此jquery函数。我毫不知道语法会是什么。任何helo都会大大降低。它弹出一个窗口供用户注册。我想在用户浏览网站上的5个页面后弹出页面。我希望在会话变量中保存这个计数。从后面的代码调用jQuery函数

Html页面

<ul> 

<li><a href="#dialog" name="modal">Simple Window Modal</a></li> 

</ul> 
div id="dialog" class="window"> 

<a href="#"class="close">Close it</a> 
<br /><br /> 
Register Now its quick and Easy.<br /> 
Rules: 

     1) Items with "*" are required fields to be filled out. 
     <br /> 
    </p> 
    <asp:Label ID="lblEmail" runat="server" Text="*Your Email: "></asp:Label><asp:TextBox 
     ID="txtEmail" runat="server"></asp:TextBox> 
    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtEmail" 
     ErrorMessage="You must enter your email address."></asp:RequiredFieldValidator> 
    <br /> 
    <asp:Label ID="lblUserName" runat="server" Text="*Name: "></asp:Label> 
    <asp:TextBox ID="txtName" runat="server" Width="157px"></asp:TextBox> 
    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtName" 
     ErrorMessage="You must enter a username."></asp:RequiredFieldValidator> 
    <br /> 
    <asp:Label ID="lblCity" runat="server" Text="*City: "></asp:Label> 
    <asp:TextBox ID="txtCity" runat="server" Width="168px"></asp:TextBox> 
    <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="txtCity" 
     ErrorMessage="You must enter your location."></asp:RequiredFieldValidator> 
    <br /> 
    <asp:Label ID="lblState" runat="server" Text=" State: "></asp:Label> 
    <asp:TextBox ID="txtState" runat="server" Width="168px"></asp:TextBox><br /> 
    <asp:Label ID="lblAge" runat="server" Text="*Age: "></asp:Label> 
    <asp:TextBox ID="txtAge" runat="server" Width="165px"></asp:TextBox> 
    <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="txtAge" 
     ErrorMessage="Please enter your age to continue."></asp:RequiredFieldValidator> 
    <br /> 
    <asp:Label ID="lblSex" runat="server" Text="*Gender: "></asp:Label> 
    <asp:RadioButton ID="rdMale" runat="server" GroupName="Gender" Text="Male" /> 
    <asp:RadioButton ID="rdFemale" runat="server" GroupName="Gender" Text="Female" /> 
    <asp:Label ID="RadialLBL" runat="server"></asp:Label> 
    <br /> 
    <br /> 
    <asp:Label ID="lblYahoo" runat="server" Text="Yahoo ID: "></asp:Label> 
    <asp:TextBox ID="YahooID" runat="server"></asp:TextBox> 
    <br /> 
    <asp:Label ID="lblMSN" runat="server" Text="MSN ID: "></asp:Label> 
    <asp:TextBox ID="MSNID" runat="server" Width="133px"></asp:TextBox> 
    <br /> 
    <br /> 
    <asp:Label ID="lblMyspaceLink" runat="server" Text="Your Myspace Link: "></asp:Label> 
    <asp:TextBox ID="MyspaceLink" runat="server" Width="255px"></asp:TextBox> 
    <br /> 
    <asp:Label ID="lblFaceBookLink" runat="server" Text="Your FaceBook Link: "></asp:Label> 
    <asp:TextBox ID="FaceBooklink" runat="server" Width="244px"></asp:TextBox> 
    <br /> 
    <asp:Label ID="lblUploadpic" runat="server" Text="*Upload your picture."></asp:Label> 
    <asp:FileUpload ID="FileUpload1" runat="server" Height="22px" Width="217px" /><br /> 
    <br /> 
    <asp:Label ID="Label1" runat="server"></asp:Label><br /> 
    <br /> 
    <asp:Label ID="lblDesctiption" runat="server" Text="*User Bio: "></asp:Label><br /> 
    <asp:TextBox ID="txtDescription" runat="server" MaxLength="240" Width="390px" Height="73px" 
     Rows="3" TextMode="MultiLine"></asp:TextBox><br /> 
    <br /> 
    <asp:Button ID="btnRegister" runat="server" Text="Register" OnClick="btnRegister_Click" /> 
    <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ControlToValidate="txtDescription" 
     ErrorMessage="Enter a bio or description of yourself."></asp:RequiredFieldValidator> 
    <br /> 
    <asp:Label ID="EnterAll" runat="server"></asp:Label> 
    <asp:Label ID="Displayme" runat="server" Text=""></asp:Label> 
    <br /> 
    <br /> 
</div> 

jQuery函数

<script> 

$(document).ready(function() { 

    //select all the a tag with name equal to modal 
    $('a[name=modal]').click(function (e) { 
     //Cancel the link behavior 
     e.preventDefault(); 

     //Get the A tag 
     var id = $(this).attr('href'); 

     //Get the screen height and width 
     var maskHeight = $(document).height(); 
     var maskWidth = $(window).width(); 

     //Set heigth and width to mask to fill up the whole screen 
     $('#mask').css({ 'width': maskWidth, 'height': maskHeight }); 

     //transition effect  
     $('#mask').fadeIn(1000); 
     $('#mask').fadeTo("slow", 0.8); 

     //Get the window height and width 
     var winH = $(window).height(); 
     var winW = $(window).width(); 

     //Set the popup window to center 
     $(id).css('top', winH/2 - $(id).height()/2); 
     $(id).css('left', winW/2 - $(id).width()/2); 

     //transition effect 
     $(id).fadeIn(2000); 

    }); 

    //if close button is clicked 
    $('.window .close').click(function (e) { 
     //Cancel the link behavior 
     e.preventDefault(); 

     $('#mask').hide(); 
     $('.window').hide(); 
    }); 

    //if mask is clicked 
    $('#mask').click(function() { 
     $(this).hide(); 
     $('.window').hide(); 
    }); 

}); 

下面是一个示例代码段脚本注册到Response

if(Session["_PageVisitCount"].ToString() == "5"){ 
    Page.ClientScript.RegisterStartupScript(
     this.GetType(), 
     "scriptsKey", 
     "yourScriptMethodToCall();", 
     true 
    ); 
} 

经过进一步审查你的代码,没有任何客户端代码,可以明确地被调用。你是否缺少任何脚本?

+0

我如何更改jquery代码,使其能够像调节器函数一样调用? – CsharpBeginner 2012-01-02 01:07:57

+0

更正,在JavaScript中创建一个功能,用于“弹出窗口”。然后,当条件正确时,请按照我所举例说明的方式注册脚本。 – xandercoded 2012-01-02 01:09:55

它不必在代码后面,你可以将代码包装在你的ASPX文件的代码块中;

<% if(((int)Session["PageViews"]) > 5) { %> 
    <script type="text/javascript"> 
    $(document).ready(function() { 
     // This code will only execute when PageViews > 5 
    }); 
    </script> 
<% } %> 

在这里,我们刚刚在服务器端检查中包装所有代码。如果检查返回false,代码将不会呈现给页面。

你不能从后面的代码中调用js脚本。但是,正如其他人所表明的那样,当响应被发送回客户端时,您可以注册一个脚本来调用jquery函数。