Open popup when specific link

0

I have to open a popup on a specific page.

My JS looks like this:

if($('.popup-banner').length > 0) {
    $('.popup-banner .fechar, .popup-banner .link-fechar, .popup-overlay').click(function() {
        $('.popup-overlay, .popup-banner').fadeOut(300);
    });
}

It is currently opening on any page I visit. But, I want to open only when I'm at home, the home address is www.teste.com.br

    
asked by anonymous 13.03.2018 / 14:19

2 answers

4

You can do both with window.location.href and check the URL, for example:

if ( !!location.href.match("https?:\/\/pt.stackoverflow.com\/?$") ) {
    alert("Página inicial")
}

Or you can do with window.location.pathname and check the path of the URL, for example:

if ( !!window.location.pathname.match("(?:\/|index\.(?:php|asp|html?))$") ) {
    alert("Página inicial")
}

Explanation of Regex 1:

https?:\/\/pt.stackoverflow.com\/?$
   └┬┘                         └─┬─┘
    │                            └──── Informa que a URL tem que terminar com '/'. http://pt.stackoverflow.com/ ou https://pt.stackoverflow.com/
    └───────────────────────────────── Informa que o 's' como opcional. Isso serve tanto para http://, quanto https://

Explanation of Regex 2:

(?:\/|index\.(?:php|asp|html?))$
 └┬┘ └───────────┬──────────┘
  │              │             
  │              │            
  │              └──────────────── ou terminar com index.php; index.asp; index.html; ou index.htm
  └─────────────────────────────── Informa que o 'path' deve terminar com '/'
  

The !! is used to convert the result to Boolean.

If you want something much simpler, you can use slice , for example:

if (location.pathname.slice(-1) === "/") {
    alert("Página Inicial");
}
    
13.03.2018 / 14:44
2

One way is to get the variable location.href (current URL) and remove www. , http:// , https:// , and / . If the result is just teste.com.br , you're on the home page:

var url_ = "http://www.teste.com.br/"; // apenas para teste. Apague esta linha
//var url_ = location.href; // descomente esta linha
url_ = url_.replace(/www.|https|http|:|\//g,"");

if($('.popup-banner').length > 0 && url_ == "teste.com.br") {
   
   $("div").show(); // apenas para exemplo. Apague esta linha
   
    $('.popup-banner .fechar, .popup-banner .link-fechar, .popup-overlay').click(function() {
        $('.popup-overlay, .popup-banner').fadeOut(300);
    });
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><divclass="popup-banner" style="display: none;">popup página inicial</div>
    
13.03.2018 / 14:54