簡體   English   中英

如何僅在首頁中加載JavaScript彈出窗口? 顯示在所有頁面

[英]How do i load javascript popup only in home page? It is displayed in all page

這是我的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(500); 
    $('#mask').fadeTo("slow",0.9);  

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

});

您可以添加此檢查:

$(document).ready(function() {  
    if (window.location.match(/www\.infoways\.com\.np$/) || window.location.match(/www\.infoways\.com\.np\/$/)) {
        var id = '#dialog';
        // more of your code
    }
});

如果網址以www.infoways.com.npwww.infoways.com.np/結尾,您的代碼將被執行

解決方案是在var id = '#dialog';正上方添加條件語句var id = '#dialog'; 步驟:

  1. 將您的首頁網址存儲在一個變量中(或像下面的具體示例一樣直接寫上它)
  2. 使用location.href比較/檢查當前URL

這是具體示例:

$(document).ready(function() {  

    if (location.href !== "http://www.infoways.com.np/") {      // Add correct protocol for your url
        return;
    }

    var id = '#dialog';

    ...

將代碼放在首頁html中的<script>標記中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM