2016-07-24 27 views
2

我知道這個問題問了好幾次,我做了一個小小的研究,並添加了我的代碼,還在某處我做錯了,現在對我來說。以下指出我需要達到的目標。 1.打開主頁上的彈出窗口。 2. Popup應該位於所有瀏覽器的中心。 3.彈出窗口應淡入。 4.應該只對一個用戶打開一次。在bigcommerce中打開Popup一次

這是我的測試網站http://popuptest.mybigcommerce.com/

到目前爲止彈出開放,對我來說,在彈出中心,並期待淡入工作。但會話不工作。

下面是我工作

index.html中

<div id="boxes"> 
    <div style="top: 199.5px; left: 551.5px; display: none;" id="dialog" class="window"> 
     <img src="images/coupon2011.jpg" width="507" height="300" /> 
    <a href="#" class="close"><img src="images/closelabel.gif" width="66" height="22" /></a> 
</div> 
<!-- Mask to cover the whole screen --> 
<div style="width: 1478px; height: 602px; display: none; opacity: 0.8;" id="mask"></div> 
</div> 
在htmlhead.html

現在使用的代碼,下面的代碼。

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js"></script> 
<script type="text/javascript"> 
$(document).ready(function() { 

    var id = '#dialog'; 

    //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> 

<script type="text/javascript"> 

var once_per_session=1 

function get_cookie(Name) { 
var search = Name + "=" 
var returnvalue = ""; 
if (document.cookie.length > 0) { 
offset = document.cookie.indexOf(search) 
if (offset != -1) { // if cookie exists 
offset += search.length 
end = document.cookie.indexOf(";", offset); 
if (end == -1) 
end = document.cookie.length; 
returnvalue=unescape(document.cookie.substring(offset, end)) 
} 
} 
return returnvalue; 
} 

function loadpopunder(){ 
if (get_cookie('popunder')==''){ 
loadpopunder() 
document.cookie="popunder=yes" 
} 
} 
function loadpopunder(){ 
if (once_per_session==0) 
loadpopunder() 
else 
{ 
if (get_cookie('popunder')==''){ 
loadpopunder() 
document.cookie="popunder=yes" 
} 
} 
} 
</script> 

任何建議或教程將是偉大的完成。

+0

我注意到,當你說「彈出」和「隱藏彈出式」它看起來像你只是實施模態對話。這種性質的真正彈出窗口現在已被主流瀏覽器的彈出式窗口攔截器阻止。 – ceejayoz

回答

1

您可以使用JavaScript庫js-cookie。藉助此庫,您可以輕鬆設置並獲取Cookie。

如果你使用JS-餅乾您htmlhead.html代碼應該是這個樣子:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js"></script> 
<script type="text/javascript"> 
$(document).ready(function() { 
    // Check if cookie exists 
    if (Cookies.get('popunder')) { 
     return; 
    } 

    var id = '#dialog'; 

    //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);  

    // Set cookie to be sure the popover activated again 
    Cookies.set('popunder', '1'); 

    //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> 
+0

它的工作....... – user2829218