Please upgrade here. These earlier versions are no longer being updated and have security issues.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.
How To Only Have JS code on Home Page/Index Only?
us3r
New
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.
0
Comments
You need to create a plugin.
❌ ✊ ♥. ¸. ••. ¸♥¸. ••. ¸♥ ✊ ❌
Was little experiencing with this question and i made a little plugin which seems to work fine: The js is only loaded in the HEAD tag when on the discussionspage(homepage)
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:
you could also use
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)
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)
I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.
Thanks for all the responses. Will try the posted methods when I get the chance. Thanks again.
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.
I would definitely give pockets a try first if I were @us3r . As for my profile photo, I am the handsome one.
Maybe Bleistivts HTMLEdit is another option
Yup, exactly what I needed, thanks.
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.
so pockets alone will not work!
you either have to use a theme tpl mod that I suggested before.
modification to your themes tpl
// add your js here.
-see jacks suggestion and create a plugin folder named HomePageOnly
plugins/HomePageOnly/default,php
http://vanillaforums.org/discussion/comment/235347/#Comment_235347
except change in default,php
to
and in homepageonly.js
plugins/HomePageOnly/js/homepageonly.js
the add your js to a file called homepageonly.js
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
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.
Perfect, thanks
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
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.
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.