How Do I Disable a Jquery Script While in Edit Mode?
Permalink 1 user found helpful
Hi, I am developing a theme, and it requires a jquery script to function. I am calling it in elements/header.php. However, I need to know how to make it so that it auto-disables when the user is in edit mode. What is the code for me to do this? Here is my current code:
<script type="text/javascript" src="<?php echo $this->getThemePath()?>/js/jquery.masonry.min.js"></script> <script> $(function(){ $('#masonry').masonry({ itemSelector: '.box', columnWidth: 100, isAnimated: true }); }); </script>
or,
if(!CCM_EDIT_MODE) { //your stuff }
The script I'm calling in from the header applies a simple effect to a class of divs within the layout (affects their arrangement). It causes difficulty with adding new blocks, so the script needs to be disabled while in edit mode. I'm afraid I don't fully understand what to do here. Can you explain step-by step what I need to do?
Replace this:
With:
<script type="text/javascript" src="<?php echo $this->getThemePath()?>/js/jquery.masonry.min.js"></script> <script> $(function(){ $('#masonry').masonry({ itemSelector: '.box', columnWidth: 100, isAnimated: true }); }); </script>
With:
<?php if(!CCM_EDIT_MODE) { ?> <script type="text/javascript" src="<?php echo $this->getThemePath()?>/js/jquery.masonry.min.js"></script> <script> $(function(){ $('#masonry').masonry({ itemSelector: '.box', columnWidth: 100, isAnimated: true }); }); </script> <?php } ?>
Hmm, tried that now, and now the script doesn't seem to work when NOT in edit mode.
just do this then:
<?php if(!$c->isEditMode()) { ?> <script type="text/javascript" src="<?php echo $this->getThemePath()?>/js/jquery.masonry.min.js"></script> <script> $(function(){ $('#masonry').masonry({ itemSelector: '.box', columnWidth: 100, isAnimated: true }); }); </script> <?php } ?>
THANK YOU! That worked perfectly! I'm just curious: does this solution work for all versions of concrete?
yes
Where To add this code I mean to say in which file, I am new to concrete5
Generally, it would be in the header.php file, before the <body> tag.
don't mind my quesiton below. I found out how to do it: create a view.js file in your block's js folder. in it use the code Mnkras gave on the very top: !if (CCM_EDIT...
that solved it :-)
thank you guys - another lesson learned.
Tobi
it's been a while but i just faced the same issue.
i added the code given by Mnkras to a view.php of a block. that would load the script not in the header but in the flow of the body.
however that did not work.
when placing the code in the header.php it did work.
does anybody know why that is so and how to place the script code as part of the view.php to avoid having it on every page even when it's not needed? (it's not needed on every page as the js files are only loaded when the corresponding block is on the page)
until then the code stays where it is (in the header) but an answer would be helpful :-)
Cheers,
Tobi
that solved it :-)
thank you guys - another lesson learned.
Tobi
it's been a while but i just faced the same issue.
i added the code given by Mnkras to a view.php of a block. that would load the script not in the header but in the flow of the body.
however that did not work.
when placing the code in the header.php it did work.
does anybody know why that is so and how to place the script code as part of the view.php to avoid having it on every page even when it's not needed? (it's not needed on every page as the js files are only loaded when the corresponding block is on the page)
until then the code stays where it is (in the header) but an answer would be helpful :-)
Cheers,
Tobi
then in javascript elsewhere on the pages using the theme you can just use: