We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Linking to Javascript in Module Front-End

Development and Programming

Nazreen's avatar
Nazreen
3 posts
14 years ago
Nazreen's avatar Nazreen

Hi everyone,

I have a little problem trying to link to custom javascript from the front-end of my module. I am familiar with this, but it only seems to include the js in the control panel end of tje module. $this->EE->cp->load_package_js(‘jquery.easing-1.3.pack’);

My module contains a number of custom js functions that I would like the front-end to have access to.

Unfortunately, I cannot link to the third party folder using PATH_THIRD as this reveals the server file structure and is a security issue.

Is this even possible?

       
Wes Baker's avatar
Wes Baker
343 posts
14 years ago
Wes Baker's avatar Wes Baker

Nazreen,

I’ve seen two ways that folks handle this:

  1. Have them include the javascript and CSS in the template themselves and have all of those files stored within the themes directory
  2. Use file_get_contents to get the contents of those files and include them inline

For an example of the second option, take a look at SafeCracker, specifically safecracker/libraries/safecracker_lib.php around line 932:

//add datepicker class
if ($this->datepicker)
{
    $this->head .= '<style type="text/css">.hasDatepicker{background:#fff url('.$this->EE->config->item('theme_folder_url').'cp_themes/default/images/calendar_bg.gif) no-repeat 98% 2px;background-repeat:no-repeat;background-position:99%;}</style>';
    $this->head .= '[removed]$.createDatepickerTime=function(){a=new Date();b=a.getHours();c=a.getMinutes();if(c<10){c="0"+c;}if(b>12){b-=12;d=" PM";}else if(b==12){d=" PM";}else{d=" AM";}EE.date_obj_time=" \\'"+b+":"+c+d+"\'";};$.createDatepickerTime();[removed]';
}

//make head appear by default
if (preg_match('/'.LD.'safecracker_head'.RD.'/', $return))
{
    $return = $this->EE->TMPL->swap_var_single('safecracker_head', $this->head, $return);
}
else if ($this->bool_string($this->EE->TMPL->fetch_param('safecracker_head'), TRUE))
{
    $return .= $this->head;
}

What’s happening here is they’re creating a variable and stashing all of the scripts and styles they need in it and then adding it to the end of the return variable.

Wes

       
Nazreen's avatar
Nazreen
3 posts
14 years ago
Nazreen's avatar Nazreen

Thanks for the speedy response Wes! Using file_get_contents does the trick.

For anyone else looking for this as well, here is what I have:

$output = '[removed]'.file_get_contents(PATH_THIRD.'module_folder_name/javascript/smartform.js').'[removed]';

(the forum replaces the opening & closing script tags with [removed])

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.