bgbs Posted January 14, 2015 Share Posted January 14, 2015 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. Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted January 14, 2015 Share Posted January 14, 2015 Make the JavaScript a separate file, and use PHP to modify your HTML and include the link. Quote Link to comment Share on other sites More sharing options...
CroNiX Posted January 14, 2015 Share Posted January 14, 2015 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; ?> Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted January 15, 2015 Share Posted January 15, 2015 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? Quote Link to comment Share on other sites More sharing options...
Zane Posted January 15, 2015 Share Posted January 15, 2015 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.