Open Fancybox on specific page

0

I have a fancybox banner that opens on all pages. However, I want it to open only when the user is on the Home page of the site.

I'm calling the javascript below in a .php file that makes the script call on every page of the site. So I just put it in the home.php file that only serves to the Home of the site. Well, I do not want Fancybox to show up on other pages. Home only.

However, the fancybox is not being called.

<script type="text/javascript">
     $(document).ready(function() {
         $.fancybox.open({
           src : '/assets/images/banner-aviso.png',
           type : 'image'
         });
     });
</script>
<a class="hidden-link pop-up" href="/assets/images/banner-aviso.png">&nbsp;</a>

On the console, the following warning appears:

  

"Uncaught ReferenceError: $ is not defined at (index): 36 (anonymous)   @ (index): 36 "

What would be the "$(document).ready(function() {"

Is JS not being called on the Home page? How would I do a PHP check to see what the page's URL is?

    
asked by anonymous 26.12.2017 / 18:15

2 answers

1

You can check which page is using $_SERVER['REQUEST_URI'] and checking with strpos() if the /home string is in the current URL.

It's also important to check if jQuery is loading on the page. Ideally, you should load it into <head> , before all plugins . The basic structure would look like this:

<html>
<head>
    <script src="jquery.js"></script>
</head>
<body>

<?php
if (strpos($_SERVER['REQUEST_URI'], '/home')){
    // está na home. Aqui você carrega os scripts que quiser apenas na home
?>
<script src="caminho_do_fancybox/fancybox.js"></script>
<script type="text/javascript">
$(document).ready(function() {
    $.fancybox.open({
       src : '/assets/images/banner-aviso.png',
       type : 'image'
    });
});
</script>
<a class="hidden-link pop-up" href="/assets/images/banner-aviso.png">&nbsp;</a>
<?php
}
?>    

</body>
</html>
    
26.12.2017 / 19:35
0

You can use parse_url to check $_SERVER['REQUEST_URI']

Note 1: error:

  

"Uncaught ReferenceError: $ is not defined at (index): 36 (anonymous) @ (index): 36"

Indicates that jQuery was not loaded on your page, you need to put it before the script you want to run, something like:

<html>
<head>
    <script src="pasta/jquery.js"></script>
    <script src="pasta/fancybox.js"></script>
</head>
<body>
    ...
    <?php if (<condição>): ?>

    <script type="text/javascript">
    $(document).ready(function() {
        $.fancybox.open({
           src : '/assets/images/banner-aviso.png',
           type : 'image'
        });
    });
    </script>
    <a class="hidden-link pop-up" href="/assets/images/banner-aviso.png">&nbsp;</a>

    <?php endif; ?>
    ...
</body>
</html>

Note 2: REQUEST_URI must have the apostrophes (single quotes), because if you do $_SERVER[REQUEST_URI] PHP will first look for a constant called REQUEST_URI , which does not exist, then it will issue a notice like this:

  

Notice: Use of undefined constant REQUEST_URI - assumed 'REQUEST_URI'

The assumed 'REQUEST_URI' indicates that PHP does not find the constant cast for string, however note that although $_SERVER[REQUEST_URI] works, but maybe some additional script generates the constant REQUEST_URI with some \ value , then this will fail for sure.

Note also that strpos() can return 0 or false , in case of the @DVD script, not even if you are accessing a page with the similar name foobarhome.php strpos will also enter in IF , then the ideal to avoid problems is to use parse_url or else use preg_match , follow examples:

Using parse_url

The parse_url($url, PHP_URL_PATH) will only extract the path , if it is in the home the value /home

<?php if (parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH) === '/home'): ?>

<script type="text/javascript">
     $(document).ready(function() {
         $.fancybox.open({
           src : '/assets/images/banner-aviso.png',
           type : 'image'
         });
     });
</script>
<a class="hidden-link pop-up" href="/assets/images/banner-aviso.png">&nbsp;</a>

<?php endif; ?>

If the home you refer to is index.php, then do so:

<?php if (parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH) === '/'): ?>
...

If it is a range of pages, you can use an array:

  

Note: ltrim takes the start bar

<?php
$pathAtual = ltrim(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), '/');

$permitidos = array( 'home', 'contato', 'admin/foo' ); //Adicione as páginas permitidas aqui
?>

<?php if (in_array($pathAtual, $permitidos)): ?>
...

With preg_match (regex)

In this case I've used (regular expressions), with it you can create a range of pages within the regex, for example this regex will check if it is the home or index #/(index|home)$# , example:

<?php if (preg_match('#/(index|home)$#', $_SERVER['REQUEST_URI'])): ?>

<script type="text/javascript">
     $(document).ready(function() {
         $.fancybox.open({
           src : '/assets/images/banner-aviso.png',
           type : 'image'
         });
     });
</script>
<a class="hidden-link pop-up" href="/assets/images/banner-aviso.png">&nbsp;</a>

<?php endif; ?>
    
26.12.2017 / 20:16