我有稱爲頁面視圖的會話變量。當我的頁面加載時,我有一個檢查,看它是否等於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>
這是最簡單的,需要最少量的更改,但這也會觸發附加到該鏈接的任何其他事件(例如,單擊跟蹤分析)。如果你想避免這種嘗試我的解決方案。 – mVChr 2012-01-02 17:29:03