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

Adding custom javascript to a rendered document in extension

Development and Programming

a la mode's avatar
a la mode
168 posts
13 years ago
a la mode's avatar a la mode

I’m working on an extension and one of the options available in the settings needs a custom javascript to be added to the document head when rendered. The problem I am having is with the parsing order. (There may also be a better way of doing the include too)

I am using the channel_entries_tagdata hook.

Inside this, once the settings are processed, I am doing the following:

// Add the required javascript
 $jscript = "
  < script type="text/javascript">
   /*! etc......
  < /script></head>
";

// Add js
 $tagdata = str_replace('</head>', $jscript, $tagdata);

I would like to be able to just keep my javascript in a separate file and include it somehow by reference, but I don’t know how to do that at this stage.

The other issue I am running into is the parsing order of the EE variables. Inside the javascript, I am using the variables from the $tagdata. Something like this:

$.post("URL", { channel: "{channel}", entryId: "{entry_id}", urlTitle: "{url_title}", lastSegment: "{last_segment}", editDate: eo.editDate, field: eo.eleName }, function(data){...

How would I call/use the EE variables in this case?

Thanks in advance.

Brian

       
Man With A Peg's avatar
Man With A Peg
124 posts
13 years ago
Man With A Peg's avatar Man With A Peg

Hey Brian,

You are barking up the wrong tree with your str_replace() call. The channel_entries_tagdata hook is called for each row returned by exp:channel:entries (and the related entries also). It does not send you any HTML, let alone the HTML <head> tags…

The way that most add-on developers are inserting JavaScript is through the Accessory add-on type (which can be used alongside your extension). Any customization is performed through the session class’s flashdata abilities.

(Sidenote: It appears to be safe to use the cp_js_end hook with the sessions class as long as you clear the session flashdata yourself. Regardless of which method you choose, it will probably take some time to sort out everything that is going on “under the hood.” Customizing JS output in the way you want is more complicated than it should be.)

       
a la mode's avatar
a la mode
168 posts
13 years ago
a la mode's avatar a la mode

Thanks Epic. I’ll take a look through that hook and see what all I need to do.

       

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.