2012-01-02 79 views
1

我有稱爲頁面視圖的會話變量。當我的頁面加載時,我有一個檢查,看它是否等於2.如果它是我想要執行代碼打開一個窗口。現在我可以點擊一個鏈接打開該窗口,它工作正常。我怎樣才能讓它在頁面加載時自動打開。在頁面加載時調用jquery函數

jQuery和會議Checkc代碼在我的網頁

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.pack.js"></script> 
<% if (Session["PagesViewed"].ToString() == "2") 
    { %> 
<script type="text/javascript"> 
    $(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(); 
     }); 

    }); 
</script> 

<% } %> 

這個鏈接是用來打開網頁。但我想在頁面加載時自動執行此操作。

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

回答

1

變化

$('a[name=modal]').click(function (e) { 
     //Cancel the link behavior 
     e.preventDefault(); 

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

 //Get the A tag 
     var id = $('a[name=modal]').attr('href'); 

並取出});

$(id).fadeIn(2000); 

    }); 
1

這裏是你如何可以自動觸發錨的單擊事件:

$("a[name='modal']").click(); 
+0

這是最簡單的,需要最少量的更改,但這也會觸發附加到該鏈接的任何其他事件(例如,單擊跟蹤分析)。如果你想避免這種嘗試我的解決方案。 – mVChr 2012-01-02 17:29:03

0
$(document).ready(function() { 

    var openModal = function (e) { 
     // Cancel the link behavior if click event 
     e && e.preventDefault(); 

     // Get the A tag or default to modal link href 
     var id = $(this).attr('href') || $('a[name=modal]').attr('href'); 

     // ...REST OF CODE FROM FUNCTION... 
    }; 

    $('a[name=modal]').click(openModal); 

    openModal(); 

}); 
0

將代碼加載內模態窗口$(document).ready(function() {})

喜歡的東西:

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.pack.js"></script> 
<% if (Session["PagesViewed"].ToString() == "2") 
{ %> 
<script type="text/javascript"> 
    $(document).ready(function() { 

     //Get the A tag 
     var id = $('a[name=modal]').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(); 
    }); 

    }); 
</script> 
相關問題