Using jQuery for page specific action

Author
Topic
#3217

I have a sidebar set as “closed” and “site-wide”. On one page I added script to “hide” the sidebar. What I now find is that when I insert any recommended script, I get the same result. It just breaks and no sidebar shows on the page. So, when I attempted to add a new page with the sidebar set to “open” the page does not show a sidebar at all. So my “hide” attempt was not really successful, just a break.
The page where I am deliberately hiding is here: http://www.rosssolargroup.com/friends/

I notice that the Smart Sidebar script is the last to load on the page.Is it a sequence problem?I am not sure how to cause my script changes to load last.

Just to be clear: When I put the script to “open” the sidebar on my new page using the same method as the page linked above, it just breaks and no sidebar shows at all.
Thanks.

Viewing 10 replies - 1 through 10 (of 10 total)
Author
Replies
  • #3219

    You have two errors:

    1. You must add your jQuery custom code after the code added by the plugin, or it will have no effect.
    2. Your code was broken, it is missing jQuery function. You have this in your page:

    [scode lang=”javascript”][/scode]

    It should be:

    [scode lang=”javascript”][/scode]

    Milan

  • #3220

    Thank you for the correction. I did it deliberately just to stop the sidebar from working.

    Do you have any insight into ways or tricks to make the jQuery script load last? As you can see, I am able to make it load. But your plugin loads later. I tried adding the action (wp_enqueue_script) to functions.php with the action called only for the page ID. Which seems to load it. But at the wrong time.

    Thanks for any further insight.

  • #3221

    jQuery must not load last. It has to load before any other script that uses jQuery. WP Enqueue Script function will always attempt to load it before anything else that has jQuery set as dependency.

  • #3241

    I am still struggling to get the custom jQuery code to load after your plugin. You load just before </body> </html>

    Even trying to load with the footer doesn’t work. Do you have a tip?

    Is a second approach to set your plugin to “custom” and add the specific script for load/hide/open/close to each page?

    Thanks for the help.
    John

  • #3242

    Code in your page is invalid as I have pointed before, you have this added to your page:

    [scode lang=”javascript”][/scode]

    And it should be:

    [scode lang=”javascript”][/scode]

    Milan

  • #3243

    I understand the correct structure. I am leaving it incorrect because is “breaks” the action on the page and causes the sidebar to not appear. Same effect as “hide.”

    Since your plugin loads AFTER the footer and just before </body> </hmtl> I have not been able to set my script to run AFTER your script runs. I tried setting a high priority number but it didn’t run after yours. So, as you said before, the “hide” request is without meaning because the default script to “show” runs last.

    If you have a suggestion how to run last in priority it would be helpful. That was my request.

    Thank you.

  • #3244

    Best way to do this is to add it to theme footer.php file before the BODY tag closes.

  • #3246

    I used your suggestion, but I am still “breaking” the plugin instead of modifying the action on one page. I added the following to my footer.php file. The page I want to “hide” the drawer on is post=2286, but when I add this code the drawer disappears on all pages.

     <?php if(is_page( 2286 ): ?>
    		<script>jQuery(document).ready(function($) {jQuery("#sss-sidebar-source-div-1").smartTabDrawer("hide");});</script>
    		
    		<?php endif ?> 

    I added it both immediately before the wp_footer call and immediately after it. In both cases, it stops the entire loading of the footer which includes your script which runs just before closing BODY. And there is no </body> when adding this code. If you have any more insight into how to specify the hide action, I appreciate it. I have used the is_page before on this theme, so I expect it to work again. Thanks, John

  • #3247

    I got my syntax correct and the is_page is now working when I add it AFTER the wp_footer call.

    If I want to make an exception for having the drawer OPEN on load, do I follow the same protocol? I tried using the same is_page for the other page where I want the drawer to open on load, but it stays closed. Looking at the page, the script appears but it doesn’t cause the drawer to open. You can see it on the home page http://www.rosssolargroup.com

    Script I added:

     <?php if(is_page(429)): ?>
    			<script type="text/javascript">jQuery(document).ready(function($) {jQuery("#sss-sidebar-source-div-1").openOnLoad("true");});</script>
    		
    		<?php endif; ?> 

    Is there another way to reverse the “false” to “true” on page load?

  • #3250

    I don’t know where you found “openOnLoad” method, but there is no such thing in the plugin. There is option for this during init, but no method for that. When you set up the sidebar there is a panel for each sidebar where you can see jQuery functions for opening or closing from code each sidebar.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Using jQuery for page specific action’ is closed to new replies.