Jump to content

Recommended Posts

I have this javascript code that loads in the footer. I only need this javascript to load on a static home page, not on all other pages.

 

<script type="text/javascript">




var banner = (function(){ 
    $('.banner').unslider({
        speed: 1000,               //  The speed to animate each slide (in milliseconds)
        delay: 999000,              //  The delay between slide animations (in milliseconds)
        complete: function() {},  //  A function that gets called after every slide animation
        keys: false,               //  Enable keyboard (left, right) arrow shortcuts
        dots: false,               //  Display dot navigation
        fluid: true             //  Support responsive design. May break non-responsive designs
    }); 
    $( '#nav li:has(ul)' ).doubleTapToGo();
}); 




 </script>
<script>
jQuery(document).ready(function() {
jQuery('.nav-toggle').click(function(){
//get collapse content selector
var collapse_content_selector = jQuery(this).attr('href'); 


//make the collapse content to be shown or hide
var toggle_switch = jQuery(this);
jQuery(collapse_content_selector).toggle(function(){
if(jQuery(this).css('display')=='none'){
toggle_switch.html('View All Services');//change the button label to be 'Show'
}else{
toggle_switch.html('Hide Services List');//change the button label to be 'Hide'
}
});
});


}); 
</script>
<script>
jQuery(document).ready(function() {
jQuery('.navtop-toggle').click(function(){
//get collapse content selector
var collapse_content_selector = jQuery(this).attr('href'); 


//make the collapse content to be shown or hide
var toggle_switch = jQuery(this);
jQuery(collapse_content_selector).toggle(function(){
if(jQuery(this).css('display')=='none'){
toggle_switch.html('Menu');//change the button label to be 'Show'
}else{
toggle_switch.html('Hide Menu');//change the button label to be 'Hide'
}
});
});


}); 
</script>

So far I have this php statement, but I'm having trouble wrapping it in this php code. 

 <?php if ($page=="home") echo " "; ?>

I'm not sure if putting the whole javascript code above is a good idea in the echo quotes.  Is there an easier or better way of wrapping javascript in PHP?

 

Your help appreciated.

 

Link to comment
https://forums.phpfreaks.com/topic/293924-wrap-javascript-in-php-if-statement/
Share on other sites

Yes, I'd also put all of the js into a separate file.  Then in your footer HTML, just do something like:

<?php if ($page=="home"): ?>
<script type="text/javascript" src="http://yoursite.com/js/your_footer_script.js"></script>
<?php endif; ?>

 

Yes, I'd also put all of the js into a separate file.  Then in your footer HTML, just do something like:

<?php if ($page=="home"): ?>
<script type="text/javascript" src="http://yoursite.com/js/your_footer_script.js"></script>
<?php endif; ?>

A little off topic, but do you recommend src="http://yoursite.com/js/your_footer_script.js" or src="/js/your_footer_script.js"?  If the first, please explain why.

 

Also, I don't think I ever used endif, but always wrap my if statements with curly brackets.  What are the advantages of one syntax over the other?

There are also HEREDOCs

echo <<<JAVASCRIPTCODE
<script type="text/javascript">

var banner = (function(){ 
    $('.banner').unslider({
        speed: 1000,               //  The speed to animate each slide (in milliseconds)
        delay: 999000,              //  The delay between slide animations (in milliseconds)
        complete: function() {},  //  A function that gets called after every slide animation
        keys: false,               //  Enable keyboard (left, right) arrow shortcuts
        dots: false,               //  Display dot navigation
        fluid: true             //  Support responsive design. May break non-responsive designs
    }); 
    $( '#nav li:has(ul)' ).doubleTapToGo();
}); 
 </script>
JAVASCRIPTCODE;

Unless you're addicted to IDE font formatting, this is something I would do.

 

More times than not though, it's a lot more tidier and organized to put the scripts in external files and include them, as someone already suggested.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.