Please upgrade here. These earlier versions are no longer being updated and have security issues.

How To Only Have JS code on Home Page/Index Only?

us3rus3r New
edited November 2015 in Vanilla 2.0 - 2.3

Hi,
I added Disqus JavaScript code to the default.master.tpl file in forum/themes/exampleTheme/Views.
Everything is fine except that the comment box loads up on every page. I only want it to appear on the home page.
Thanks for your help.

Comments

  • vrijvlindervrijvlinder Papillon-Sauvage MVP
  • peregrineperegrine MVP
    edited November 2015

    @us3r said:
    Hi,
    I added Disqus JavaScript code to the default.master.tpl file in forum/themes/exampleTheme/Views.
    Everything is fine except that the comment box loads up on every page. I only want it to appear on the home page.
    Thanks for your help.

    aside from plugin above ... (which could use public function DiscussionsController_Render_Before($Sender) )

    or if you want it theme specific (you could add function to the themehooks file

    or

    Assuming you want to modify the smarty template file

    Assuming you want to modify the smarty template file to only load the inline js on your discussions index page but only if the the url is the home page but does not contain "discussions" in the url.

    you can use this:

        {if ($BodyID =='vanilla_discussions_index')  && ($Path == "")}
          {literal}
        <div id="disqus_thread"></div>
        <script>
           /**
           put your script here.
           **/
        </script>
        <noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript" rel="nofollow">comments powered by Disqus.</a></noscript>
         {/literal} 
        {/if}
    

    you could also use

            {if  ($Path == "")}
              {literal}
            <div id="disqus_thread"></div>
            <script>
               /**
               put your script here.
               **/
            </script>
            <noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript" rel="nofollow">comments powered by Disqus.</a></noscript>
             {/literal} 
            {/if}
    

    Assuming you want to modify the smarty template file to only load the js on your discussions index page (i.e. whenever someone looks at your discussion topics)

       {if ($BodyID =='vanilla_discussions_index')}
          {literal}
        <div id="disqus_thread"></div>
        <script>
           /**
           put your script here.
           **/
        </script>
        <noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript" rel="nofollow">comments powered by Disqus.</a></noscript>
         {/literal} 
        {/if}
    

    Assuming you want to modify the smarty template file to only load the js on your discussions index page (i.e. whenever someone looks at your discussion topics or discussion topics within categories as well)

        {if ($BodyID =='vanilla_discussions_index')  || ($BodyID == "vanilla_categories_index")}
          {literal}
            <div id="disqus_thread"></div>
            <script>
               /**
               put your script here.
               **/
            </script>
            <noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript" rel="nofollow">comments powered by Disqus.</a></noscript>
             {/literal} 
            {/if}
    

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

    jackmaessenus3rvrijvlinder
  • Thanks for all the responses. Will try the posted methods when I get the chance. Thanks again.

    vrijvlinderperegrine
  • Am I missing something? Couldn't this be handled with the Pocketsame plug in?

    peregrineus3r
  • peregrineperegrine MVP
    edited November 2015

    @nervoustwit said:
    Am I missing something? Couldn't this be handled with the Pocketsame plug in?

    Oh pockets plug in. perhaps the easiest, depending on what OP really wants to do for what reason, albeit, it might be a bit more overhead then a simple theme change.

    Or perhaps the person wanted it theme specific for some theme they were developing,

    For pockets, you would still need to write code to only present on a url (if that were the desire)

    (first hit on home page)

    http:/example.com

    vs. (repeated hits on discussions page)

    http://example.com/discussions

    In general, pockets would be another solution, however, the person asked about default.master.tpl (and part of the question is slightly ambiguous home page vs index page vs both vs. url ).

    there are probably a myriad of plugins and solutions for the question depending on the question specificity.

    the answers above provide the questioner some info on how to modify vanilla for there own needs and how to write some rudimentary routines,

    but your idea of pockets could be simplest solution if it were for discussions page, or one of the other pockets options, so you might not be missing something at all. all depends on OP's intent.

    Nervoustwit, your photo from your profile here and your profile link to github shows a photo that makes you look like two totally different people. Would have never guessed you were the same person :)

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

    us3r
  • I would definitely give pockets a try first if I were @us3r . As for my profile photo, I am the handsome one.

    us3r
  • R_JR_J Cheerleader & Troubleshooter Munich Moderator

    Maybe Bleistivts HTMLEdit is another option


    us3r
  • @nervoustwit said:
    Am I missing something? Couldn't this be handled with the Pocketsame plug in?

    Yup, exactly what I needed, thanks.

  • @peregrine said:

    @nervoustwit said:
    Am I missing something? Couldn't this be handled with the Pocketsame plug in?

    Oh pockets plug in. perhaps the easiest, depending on what OP really wants to do for what reason, albeit, it might be a bit more overhead then a simple theme change.

    Or perhaps the person wanted it theme specific for some theme they were developing,

    For pockets, you would still need to write code to only present on a url (if that were the desire)

    (first hit on home page)

    http:/example.com

    vs. (repeated hits on discussions page)

    http://example.com/discussions

    In general, pockets would be another solution, however, the person asked about default.master.tpl (and part of the question is slightly ambiguous home page vs index page vs both vs. url ).

    there are probably a myriad of plugins and solutions for the question depending on the question specificity.

    the answers above provide the questioner some info on how to modify vanilla for there own needs and how to write some rudimentary routines,

    but your idea of pockets could be simplest solution if it were for discussions page, or one of the other pockets options, so you might not be missing something at all. all depends on OP's intent.

    Nervoustwit, your photo from your profile here and your profile link to github shows a photo that makes you look like two totally different people. Would have never guessed you were the same person :)

    Yes, I would like it to only appear in the home page/index. With the pockets plugin, it still loads the JS in the categories (domain.com/categories). It's much better than before though.

  • peregrineperegrine MVP
    edited November 2015

    Yes, I would like it to only appear in the home page/index. With the pockets plugin, it still loads the JS in the categories (domain.com/categories). It's much better than before though.

    we don't know what your home page is. what does it point to?

    be clear please - you want it only on the discussion topics index page.

    e,g, http://vanillaforums.org/discussions

    but not something like this

    http://vanillaforums.org/categories/developers

    and not this either

    http://vanillaforums.org/categories

    post a screenshot of the options you chose for the pocket.

    How do you want it solved via theme, your own plugin, or via a modification to pockets?

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

  • peregrineperegrine MVP
    edited November 2015

    Yes, I would like it to only appear in the home page/index. With the pockets plugin, it still loads the JS in the categories (domain.com/categories).

    so pockets alone will not work!

    • tpl option

    you either have to use a theme tpl mod that I suggested before.

    modification to your themes tpl

    {if ($BodyID =='vanilla_discussions_index')}
          {literal}
          <script>
    

    // add your js here.

         </script>
         {/literal} 
        {/if}
    

    • your own plugin

    -see jacks suggestion and create a plugin folder named HomePageOnly
    - add the code to default.php within folder created above.

    plugins/HomePageOnly/default,php

    http://vanillaforums.org/discussion/comment/235347/#Comment_235347

    except change in default,php

         public function Base_Render_Before($Sender) {
    
       ..... all the code
              }
    

    to

         public function DiscussionsController_Render_Before($Sender) {
                $Sender->AddJsFile($this->GetResource('js/homepageonly.js', FALSE, FALSE));
              }
    

    and in homepageonly.js

    plugins/HomePageOnly/js/homepageonly.js

    the add your js to a file called homepageonly.js


    • pockets mod.

    enable pockets plugin but read below carefully...

    make a pocket. give it a name e.g. "samplepocketone" . DO NOT ENABLE the pocket.

    then add this to default.master.tpl

      {if ($BodyID =='vanilla_discussions_index')} 
              {pocket name="samplepocketone"}
             {/if}
    

    all three options should work if you have the time to do them.

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

    us3rjackmaessen
  • @peregrine said:

    • pockets mod.

    enable pockets plugin but read below carefully...

    make a pocket. give it a name e.g. "samplepocketone" . DO NOT ENABLE the pocket.

    then add this to default.master.tpl

      {if ($BodyID =='vanilla_discussions_index')} 
              {pocket name="samplepocketone"}
             {/if}
    

    all three options should work if you have the time to do them.

    Perfect, thanks :+1:

  • R_JR_J Cheerleader & Troubleshooter Munich Moderator

    @peregrine said:
    we don't know what your home page is. what does it point to?

    config.php knows: c('Routes.DefaultController'); ;)


  • peregrineperegrine MVP
    edited November 2015

    @peregrine said:

    we don't know what your home page is. what does it point to?

    config.php knows: c('Routes.DefaultController'); ;)

    it could have been expressed in human readable format as well. ;)
    but config never lies, except when it has version numbers in it :winky:


    so, in the long run the simplest change would have been without pockets :)

    the above custom plugin would have not required a theme change.

    a theme change wouldn't have required a plugin.

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

  • unixherounixhero
    edited November 2015

    Pocket plugin is awesome and all. But no, pockets does _NOT _ work for JS snippets. I've asked the same question before, and people has been recommending me pockets. It just doesn't work for adding code. When pasting the code into a pocket and saving, it does not get stored properly ie. the code is not intact after saving and retrieving it again.

    Would have been nice, so I could add some nice third party analytics features.

  • @us3r - perhaps you could use the AddCssAndJavascript plugin. You'd have to add your own javascript file that has a condition to see which URLs you are currently on to keep you JS code only functional on the home page.

Sign In or Register to comment.