Sushil Lamichhane
Sushil Lamichhane

Reputation: 3

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

Here is my Javascript code. How can I make it display only on the home page?

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

});

Upvotes: 0

Views: 938

Answers (3)

Matt Sadev
Matt Sadev

Reputation: 123

The solution is to add conditional statement just above var id = '#dialog';. The steps:

  1. Store your homepage url in a variable (or just write it literally like the concrete example below)
  2. Compare/Check it with current url, using location.href

Here is the concrete example:

$(document).ready(function() {  

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

    var id = '#dialog';

    ...

Upvotes: 1

jcubic
jcubic

Reputation: 66560

you can add this check:

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

your code will be executed if url ends with www.infoways.com.np or www.infoways.com.np/

Upvotes: 1

Greg
Greg

Reputation: 78

Put the code in a <script> tag in your homepage html.

Upvotes: 0

Related Questions