Fork me on GitHub

How do I include a javascript file in my module  Bottom

  • Alright, before anyone tells me to do a search...
    I have...

    I want to have a javascript file(s) included for my Module to use...
    How do I make sure that each page that uses the module, also includes "xyz.js"

    Do I just create a folder called "includes" or "javascript" and it auto-includes them? Do I create a special function that is called by pnadmin or pnuser?

    I am lost...
    If you can help? Please do...

    --


    With the advent of modern technology, sites like Twitter, and the Facebook Virus...
    I think we should all breathe a collective sigh of relief that I got here in time...
    - randomblink
  • Are you templating your module with pnRender, or using pnHTML or what?

    --
    itbegins.co.uk - Zikula Consulting

    birtwistle.me.uk - Personal Blog


    Please read the Support Guide
  • I am using the new pnRender...

    --


    With the advent of modern technology, sites like Twitter, and the Facebook Virus...
    I think we should all breathe a collective sigh of relief that I got here in time...
    - randomblink
  • Whats with the squirrels?
  • Bloody tree rats are everywhere...
    You gotta love em... Rats with great P.R.
    What's with them? That's a secret they hold tighter than their cheeks...

    But, uh, back to my question...
    If I am designing a Module and I want it to include a Javascript File, HOW do I accomplish this? Is there a special way of including via pnRender?

    Help me Mr. Popeil, I'm in trouble...

    --


    With the advent of modern technology, sites like Twitter, and the Facebook Virus...
    I think we should all breathe a collective sigh of relief that I got here in time...
    - randomblink
  • Using .7x you need to added an element to the global additional_header array. i.e

    Code

    $GLOBALS['additional_header'][] = '<script src="....." ....></script>';


    -Mark

    --
    Visit My homepage and Zikula themes.
  • Thank you VERY MUCH Mr. Mark West...

    Now a stupid question.
    WHERE do I make this call...?

    I have my module layout pretty much setup...
    It is basically the 7.5 Example Module (modified of course).
    So where do I add this line...

    ALSO...
    On a side-note.
    Is there a pnRender way of doing this?
    I assumed that whatever function needed this javascript file? I would add a line to THAT function that did something like $pnRender->addHeader( 'javascript', blah blah blah );

    Or something like that...

    Help Me Mr. Popeil...

    --


    With the advent of modern technology, sites like Twitter, and the Facebook Virus...
    I think we should all breathe a collective sigh of relief that I got here in time...
    - randomblink
  • You add to this global variable wherever you need to in your code..... This will change with .8x where we'll have the concept of page variables that can control addition to the header.

    -Mark

    --
    Visit My homepage and Zikula themes.
  • Alrighty boys and girls...
    I have a work-around that is VERY nice... and personally, it should make it easier once they fix this situation with the next upgrade.

    What I did to include a file, as needed, to a module ( or I believe it would work with a theme as well (haven't started learning the new theming process yet) ) is I created a new FUNCTION...

    In your Module's FILE STRUCTURE you will want to do the following:
    create a folder HERE:
    modules/yourmodulename/pnincludes

    Put the files you want to include inside the pnincludes folder.

    create your FUNCTION and put it HERE:
    modules/yourmodulename/pntemplates/plugins/

    Then... I named mine the following:
    function.eng_additionalheaders.php

    Here is the code I use...

    Code

    //*******************************************************************************   
    //* Smarty Function::Engineering Additional Headers                             *
    //*******************************************************************************
    //*     smarty_function_eng_additionalheaders::                                 *
    //*     Smarty Function used to add additional headers to a page for including  *
    //*     files that may be needed.                                               *
    //*******************************************************************************
    //*     Argument(Req/Opt)       Basic Description                               *
    //*     *****************       *************************************************
    //*     $params                 All attributes passed to this function from the *
    //*                             template                                        *
    //*     &$smarty                Reference to the Smarty object                  *
    //*******************************************************************************
    //*     Special Use Instructions                                                *
    //*     *********************************************************************** *
    //*     Inside a SMARTY template ( i.e., engineering_admin_new.htm ) you will   *
    //*     call this function by using the following command:                      *
    //*     *********************************************************************** *
    //*    
    //* <!--[eng_additionalheaders nameOfFile="example.css" typeOfFile="css"]-->
    //*     --- or ---
    //* <!--[eng_additionalheaders nameOfFile="example.js" typeOfFile="javascript"]-->
    //*    
    //*******************************************************************************
    function smarty_function_eng_additionalheaders( $params, &$smarty )
    {
        extract( $params );
        unset( $params );

        $modName = pnModGetName();

        $GLOBALS['additional_header'][] =
            "<script type=\"text/" . $typeOfFile . "\" src=\"modules/" . $modName . "/pnincludes/" . $nameOfFile . "\"></script>";
    }


    I believe that should cover it...
    Very simple... Very easy...
    Any questions?

    --


    With the advent of modern technology, sites like Twitter, and the Facebook Virus...
    I think we should all breathe a collective sigh of relief that I got here in time...
    - randomblink
  • Hey, thanks for this code! Now if only I could get my javascript to work. Bet it's not written right. *sigh*
  • Do you guys know of anyway of testing to ensure the function is even getting called?? I tried doing a simple print and then die but that didn't work any idea how I might do that???




    -SUNADMN
  • Well, if it didn't work, I'm thinking that means it didn't get called, because it should work.

    *tests*

    Yeah, I put a print/die thing in the function, and it printed and died.

This list is based on users active over the last 60 minutes.