I have run into the situation were I need to include an external javascript file. The current drupal_add_js
function does not support this.
I was able to very easily modify includes/common.inc
to allow for this functionality. The only other issue I have is that the javascript includes are not output in the same order they were added. That could be fixed by inserting them with an arbitrary number key before of $type
as the key.
$javascript[$scope][$type][$data] = array('cache' => $cache, 'defer' => $defer, 'preprocess' => (!$cache ? FALSE : $preprocess));
If that were changed to something like:
$javascript[$scope][][$type][$data] = array('cache' => $cache, 'defer' => $defer, 'preprocess' => (!$cache ? FALSE : $preprocess));
Then the items could be output in the same order they were input. The reason this would be nice is if you include a javascript file and then output some code it won't necessarily output the include first.
To allow for external javascript includes the following would need to be modified:
$no_preprocess[$type] .= '<script type="text/javascript"'. ($info['defer'] ? ' defer="defer"' : '') .' src="'. base_path() . $path . ($info['cache'] ? $query_string : '?'. time()) ."\"></script>\n";
The modification could look something like the following:
$no_preprocess[$type] .= '<script type="text/javascript"'. ($info['defer'] ? ' defer="defer"' : '') .' src="'. ($type == 'external' ? '' : base_path()) . $path . ($info['cache'] ? $query_string : '?'. time()) ."\"></script>\n";
I suppose it would also need to be done to preprocessed file:
$preprocessed .= '<script type="text/javascript" src="'. ($type == 'external' ? '' : base_path()) . $preprocess_file .'"></script>'."\n";
So basically just changing base_path()
to ($type == 'external' ? '' : base_path())
.
Not sure what the opinions are on this, so let me know. Would probably also be useful for 5.x, etc.
Comment | File | Size | Author |
---|---|---|---|
#5 | drupal_add_js.patch | 5.72 KB | boombatower |
#3 | drupal_add_js.patch | 5.73 KB | boombatower |
#2 | drupal_add_js.patch | 5.73 KB | boombatower |
Comments
Comment #1
gregglesUpdating version - this can't make it into 6.x but I think it would be good for 7.x
@boombatower - can you provide a patch that provides the functionality? http://drupal.org/patch/create
I think that returning them in the order they were added is a good idea.
Comment #2
boombatower CreditAttribution: boombatower commentedI created a patch that changes the necessary code in order to not only output the items in order, but also allows for type external. This will allow including of external files using the
drupal_add_js
function.As a note the code doesn't seem to work with
call_user_func_array('array_merge_recursive', ...)
around the settings array, and looking at it I'm not sure why that is there.Interestingly enough the patch makes the code simpler.
Comment #3
boombatower CreditAttribution: boombatower commentedRe-rolled.
Comment #4
beeradb CreditAttribution: beeradb commentedTested. Everything looked good, allowed for adding external javascript files and respected order.
great feature addition as well, as i'm sure we've all ran into the need for this.
Comment #5
boombatower CreditAttribution: boombatower commentedThis has been re-rolled for appending (dot) coding style changes.
Comment #6
boombatower CreditAttribution: boombatower commentedThis patch is ready to go just needs a "yeh" or "neh"
Comment #7
bcn CreditAttribution: bcn commentedI think this issue is a duplicate of http://drupal.org/node/91250
Comment #8
boombatower CreditAttribution: boombatower commentedThis patch also ensures that the javascript files are output in the same order they were added. This is good to ensure since many times subsequent includes assume previous ones.
Comment #9
boombatower CreditAttribution: boombatower commentedping?
Comment #10
Dries CreditAttribution: Dries commentedI've asked the folks that are active in http://drupal.org/node/91250 (partial duplicate) to provide their input on this issue. This will help make sure that this patch accommodates all needs. Let's give them a day or two to comment. I'll mark this as 'code needs review' but feel free to set it back to RTBC after a couple of days.
Comment #11
RobLoachhttp://drupal.org/node/251578#comment-871405