2012-01-02 3 views
1

У меня есть переменная сеанса, называемая просмотром страниц. Когда моя страница загружена, у меня есть проверка, чтобы убедиться, что она равна 2. ЕСЛИ он хочет выполнить код, чтобы открыть окно. Прямо сейчас я могу щелкнуть ссылку, чтобы открыть это окно, и он отлично работает. Как я могу заставить его автоматически открываться при загрузке страницы.Вызов функции Jquery при загрузке страницы

Jquery и Session 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> 

<% } %> 

THis ссылка используется, чтобы открыть страницу. Но я хочу сделать это автоматически, когда страница загружена.

<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

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

код Put для загрузки модального окна внутри $(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> 
Смежные вопросы