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

How do you add Javascript to the Control Panel?

Development and Programming

Mark Bowen's avatar
Mark Bowen
12,637 posts
15 years ago
Mark Bowen's avatar Mark Bowen

Hiya,

I was just wondering which is the best item to use to handle adding in some javascript to the Control Panel? I need it to be on every single page.

Should it be an Accessory (can they even do this?), an extension or something else?

Thanks.

Best wishes,

Mark

       
Bjørn Børresen's avatar
Bjørn Børresen
629 posts
15 years ago
Bjørn Børresen's avatar Bjørn Børresen

An extension, hooking onto the cp_js_end hook:

http://ellislab.com/expressionengine/user-guide/development/extension_hooks/cp/javascript/index.html

       
Mark Bowen's avatar
Mark Bowen
12,637 posts
15 years ago
Mark Bowen's avatar Mark Bowen

Hmmm,

Took a look at the page but don’t really know what to do with it :-(

If say I had a javascript in a folder on the server called my_script.js then how do I use that hook to attach the javascript? Also if creating this as an extension could I place the script in the same third_party folder as my extension?

Sorry for what is possibly a very dumb question but taking a look at that page didn’t give me much information. Please don’t get me wrong, I’m not blaming you 😉 rather the documentation.

Best wishes,

Mark

       
Bjørn Børresen's avatar
Bjørn Børresen
629 posts
15 years ago
Bjørn Børresen's avatar Bjørn Børresen

No worries, I’ve mostly figured out by trial and error and / or looking at other developers code.

Basically in the hook function you output the js, so this:

public function on_cp_js_end($data)
{
$html = 'alert("hello!");';
return $html;
}

.. would produce an alert on each and every page.

You can put js code in /themes/third_party/<your_addon>/js/blabla.js and load it in some way I guess.

My best recommendation is to check out the source of some of the addons over on devot-ee and see how they used this hook:

http://devot-ee.com/hooks/ee/cp_js_end/

       
Mark Bowen's avatar
Mark Bowen
12,637 posts
15 years ago
Mark Bowen's avatar Mark Bowen
No worries, I’ve mostly figured out by trial and error and / or looking at other developers code. You can put js code in /themes/third_party/<your_addon>/js/blabla.js and load it in some way I guess.

Yep that’s exactly what I’m trying to do.

My best recommendation is to check out the source of some of the addons over on devot-ee and see how they used this hook: http://devot-ee.com/hooks/ee/cp_js_end/

Doing that now actually 😉

Thanks for the help on this.

Best wishes,

Mark

       

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.