Okay
  Public Ticket #879785
Per Page Javascript
Closed

Comments

  •  1
    Jack started the conversation

    Hi,


    I'm trying to apply some custom Javascript to a single webpage but I notice there's no box for per-page javascript in Kalium. I tried adding the code to Theme Options -> Footer -> JavaScript & Tracking Code but this didn't work nor would it be an ideal solution.

    Here is the code:

    setInterval(function()
    {
      $('#sm_widget-2').load(document.URL +  ' #sm_widget-2');
    }, 120000);

    This will be used to refresh the element ID '#sm_widget-2' every 12 seconds.

    All I need to know is how I can apply this code to a single page?


    Thanks,


    Best Regards,

    Jack Vanstone


  • [deleted] replied

    Hi Jack,

    Actually we do not provide Custom JS for single page, however here is one way how can you achieve this:

    1. Go to Pages (or Posts) in WP admin and hover over the page you want to set the javascript to get the Page/post ID: 


    (view large version)

    2. Modify the JavaScript code you have now to work only in that page ID:

    jQuery( document ).ready( function( $ ) {
    	
    	var pageID = 4844;
    	
    	if ( $( 'body' ).hasClass( 'page-id-' + pageID ) || $( 'body' ).hasClass( 'postid-' + pageID ) ) {
    
                    // Your Custom Code	
    		setInterval(function() {
    			$( '#sm_widget-2' ).load( document.URL +  ' #sm_widget-2' );
    		}, 120000 );
                    // End: Your Custom Code
    	}
    } );

    Do not forget t o replace that 4844 with your page ID.

    3. Apply that in Custom JavaScript in Theme Options and it should work.

  •  1
    Jack replied

    Hi Arlind,

    Thanks for the explanation, I managed to get this to work in theory but sadly refreshing the div was breaking the plugin and making it show incorrect/no information. I've since spoken to the plugin author who has given me js to edit and then provided me with different javascript from my own to place into the page.

    Please can you let me know where to place this code, can I use the method you provided and just replace the custom code between the 'Your Custom Code/End Custom Code' markers?


    $(document).ready(function () {

         setInterval(function () {

           smBuildWidgets();

         }, 5000);

       });



    Thanks,

    Best Regards,

    Jack Vanstone

  • [deleted] replied

    Hi Jack,

    This is the version of code you need to apply now:

    jQuery( document ).ready( function( $ ) {
    	
    	var pageID = 4844;
    	
    	if ( $( 'body' ).hasClass( 'page-id-' + pageID ) || $( 'body' ).hasClass( 'postid-' + pageID ) ) {
    
                    // Your Custom Code	
    		setInterval(function() {
    			smBuildWidgets();
    		}, 5000 );
                    // End: Your Custom Code
    	}
    } );