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.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

greggles’s picture

Version: 6.x-dev » 7.x-dev

Updating 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.

boombatower’s picture

Assigned: Unassigned » boombatower
Status: Active » Needs review
FileSize
5.73 KB

I 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.

boombatower’s picture

FileSize
5.73 KB

Re-rolled.

beeradb’s picture

Status: Needs review » Reviewed & tested by the community

Tested. 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.

boombatower’s picture

FileSize
5.72 KB

This has been re-rolled for appending (dot) coding style changes.

boombatower’s picture

This patch is ready to go just needs a "yeh" or "neh"

bcn’s picture

I think this issue is a duplicate of http://drupal.org/node/91250

boombatower’s picture

This 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.

boombatower’s picture

ping?

Dries’s picture

I'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.

RobLoach’s picture

Status: Reviewed & tested by the community » Closed (duplicate)