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
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.)
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.