2012-01-02 128 views
0

如何從頁面後面的代碼中調用此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(); 
    }); 

}); 

回答

0

下面是一個示例代碼段腳本註冊到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

1

它不必在代碼後面,你可以將代碼包裝在你的ASPX文件的代碼塊中;

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

在這裏,我們剛剛在服務器端檢查中包裝所有代碼。如果檢查返回false,代碼將不會呈現給頁面。

0

你不能從後面的代碼中調用js腳本。但是,正如其他人所表明的那樣,當響應被髮送回客戶端時,您可以註冊一個腳本來調用jquery函數。

相關問題