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.
Options

add Javascript or jquery in default.master.tpl

I've tried to add jquery in Head at default.master.tpl.
I know a way as follows:
{literal}

jQuery(document).ready(function($) { ...... }

{/literal}

Unfortunately, i cannot operate well. I don't know what I have problems.
Do you know how to add javascript or jquery in default-master.tpl
Please let me know.
Thanks

Comments

  • Options
    422422 Developer MVP

    use the example plugin

    There was an error rendering this rich post.

  • Options

    it is pretty easy to create a themehooks file and add it there

    themes/YOURTHEME/class.themehooks.php

        class ThemeHooks implements Gdn_IPlugin {
    
           public function Setup() {
    
           }
    
           public function OnDisable() {
    
           }
    
    
    
            public function Base_Render_Before($Sender) {
                if (($Sender->MasterView == 'default' || $Sender->MasterView == '')) {
                $Sender->AddJsFile('mynew.js', 'themes/embedfriendly/js');
                }
    
         }
    
    
        }
    

    put the mynew.js in themes/EmbedFriendly/js/mynew.js

    jQuery(document).ready(function($) { ...... }

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

  • Options

    Thank you so much. I can add my javascript.

  • Options

    Unfortunately, when I add javascript with class.themehooks.php, I cannot use SSO anymore. Because jsAutoSignin include public function Base_Render_Before($Sender).
    Do you have any other solution?
    Thanks.

  • Options
    peregrineperegrine MVP
    edited March 2013

    .

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

  • Options

    What happens do you get an error message?

    I'm not sure why that would break it. Is there a problem with calling the function or with a jquery clash.

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

  • Options

    I don't have any error message. But I cannot use SSO function through jsAutosignin plugin which doesn't operate .

    jsConnectAutoSigning also includes public function Base_Render_Before(). I don't know well what I have problems.

    class jsConnectAutoSignInPlugin extends Gdn_Plugin {
    public function Base_Render_Before($Sender, $Args) {
    if (!Gdn::Session()->UserID) {
    $Sender->AddCssFile('jsconnectAuto.css', 'plugins/jsconnectAutoSignIn');
    $Sender->AddJSFile('jsconnectAuto.js', 'plugins/jsconnectAutoSignIn');
    $Sender->AddDefinition('Connecting', T('Connecting','Connecting...'));
    $Sender->AddDefinition('ConnectingUser', T('ConnectingUser','Hi % connecting you to Tapastic Forums...'));
    if (C('Plugins.jsconnectAutoSignIn.HideConnectButton') || IsMobile()) {
    $Sender->Head->AddString('.ConnectButton{display:none!important;}');
    }
    }

        if(C('Plugins.jsconnectAutoSignIn.HideSignIn')){
            $Sender->Head->AddString('<script type="text/javascript">' .
                'jQuery(document).ready(function($){' .
                    '$(\'.ConnectButton,.SignInItem,a[href*="entry/signin"],a[href*="entry/signout"]\').hide();' .
                '});' .
                '</script>');
        }
    

    Thanks for your helping.

    }
    
  • Options

    Look in google and/or firefox and see if both your js scripts are loading and if they are producing console errors..

    Perhaps you have an error in the js script you added that made everything go south.

    maybe provide a link to your site.

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

  • Options
    vrijvlindervrijvlinder Papillon-Sauvage MVP
    edited March 2013

    JQUERY and SMARTY do not get along too well, they both target the same files, but just at different times.Maybe you can do it using smarty to get the js. If you copy this to try remove the spaces I added between the < > so they would show here.

    In < head > Element of your page 
    Code:
    
    {literal} 
    < script type="text/javascript" > 
     
    function addLoadEvent(func) { 
      var oldonload = window.onload; 
      if (typeof window.onload != 'function') { 
        window.onload = func; 
      } else { 
        window.onload = function() { 
          oldonload(); 
          func(); 
        } 
      } 
    } 
    
    < /script > 
    
    {/literal} 
    < !-- HeaderContent start -- > 
    {if isset($HeaderContent)}{$HeaderContent}{/if} 
    < !-- HeaderContent end -- >
    

    In your other templates
    Code:

    {capture name=javascript} 
    {literal} 
    < script type="text/javascript" > 
    
    addLoadEvent( 
       function() 
       { 
          startMe(); 
       } 
    ); 
    
    
    < /script > 
    {/literal} 
    {/capture} 
    
    {if $smarty.capture.javascript ne ""}{assign var=HeaderContent value="`$HeaderContent``$smarty.capture.javascript`"}{/if} 
    
  • Options

    I think there is an error in the js that was added.

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

  • Options
    peregrineperegrine MVP
    edited March 2013

    edited.

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

  • Options
    vrijvlindervrijvlinder Papillon-Sauvage MVP

    what? I had no idea I will remove it presto!

  • Options

    thx.

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

  • Options

    Base_Render_Before is probably the most used hook, you can call it any number of time. What is it that you doing in you js? remember Auto Sign In requires the guest module to work.

    grep is your friend.

  • Options

    Yes. My javascript had an error. I solved it thanks to you. I really appreciate you.

  • Options

    Thank you guys! This helped out with my gallery issue! Finally!

    Love this community.

Sign In or Register to comment.