Is is possible to use drupal_add_js with more that one get parameter in the source url? When I try this the ampersand gets urlencoded. For example, the google maps api:

drupal_add_js('http://maps.googleapis.com/maps/api/js?v=3.7&sensor=true');

results in:

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?v=3.7&amp;sensor=true"></script>

Comments

lsdoc77’s picture

May you could try to escape it like this :

<?php
drupal_add_js('http://maps.googleapis.com/maps/api/js?v=3.7\&sensor=true');
?>
bkelly9376’s picture

Thanks for the reply. I've tried escaping and it doesn't make a difference.

lsdoc77’s picture

Have a look also in drupal_add_html_head .

lsdoc77’s picture

Your code doesn't work at all? Because I tested it in my computer and the map shows up fine..

revnoah’s picture

I tried the following code but it didn't work:

	$googlemaps_api_key = _googlemaps_api_key(); //returns string

	$google_script = array(
		'#tag' => 'script',
		'#attributes' => array(
			'src' => 'https://maps.googleapis.com/maps/api/js?key=' . $googlemaps_api_key . '&v=3.7&sensor=false',
			'type' => 'text/javascript'
		)
	);
	drupal_add_html_head($google_script, 'googlemaps');
Sibiraj PR’s picture

try this with a module init function


/**
 * Implementation of hook_init().
 */

function MYMODULE_init() {
drupal_set_html_head('<script type="text/javascript" src="http://maps.google.co.uk/maps/api/js?v=3.7&sensor=true"></script>');
}

plcstpierre’s picture

Does it exists another solution ?

Jaypan’s picture

You can try this, but I haven't tested to see if it stops encoding the ampersand or not:

drupal_add_js('http://maps.googleapis.com/maps/api/js?v=3.7&sensor=true', array('external' => TRUE));
revnoah’s picture

No, that previously untested code doesn't work.

revnoah’s picture

What was your final solution? I ended up modifying html.tpl.php, which I don't feel is the best way to do it. But this html/php code got me up and running at least:

<script src="https://maps.googleapis.com/maps/api/js?key=<?php echo $googlemaps_api_key; ?>&sensor=false" type="text/javascript"></script>
Jaypan’s picture

I used drupal_add_html_head() for this.

wolffereast’s picture

Ran into this post while trying to add google maps with a key just now. Here is what worked for me:
In a theme preprocess function (I used MYTHEME_preprocess_page, as it suited my purposes well) I added the following code:

$element = array(
  '#type' => 'markup',
  '#markup' => '<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false&key=GMAP_API_KEY"></script>', 
);

drupal_add_html_head($element, 'MYTHEME_google_maps_js');

I had to use an element of type markup to get the closing script tag in place, otherwise it output as <script src="..." /> instead of <script src="..."></script> which broke pretty much everything.

taggartj’s picture

ok so i was caught in this trap too ... and tried a lot of stuff

so what I did in the end was :
in template.php

drupal_add_js(drupal_get_path('theme', 'THEME_NAME') . '/main.js');

in module

/**
 * Implements hook_js_alter().
 */
function MYMODULE_js_alter(&$javascript) {
  //drupal_get_path('theme', 'THEME_NAME')
  $path = drupal_get_path('theme', 'THEME_NAME') . '/main.js';
 //used with require.js so drupal.Settings (in js ) are not set yet 
  if (isset($javascript[$path])) {
    $javascript[$path]['version'] = variable_get('js_version', 0); // PUT WHAT YOU WANT HERE
  }
}

//then in 
/**
 * Implements hook_flush_caches().
 */
//increment js_version

so every time we change stuff now in our require.js we just need to run flush cache and yes bob is my uncial

In main.js

function getRequireUpdateURLParamValue(){
//return the v=X value 
}

require.config({
    baseUrl: '/sites/all/themes/THEME/JS',

    urlArgs: 'bust=v' + getRequireUpdateURLParamValue(),
    waitSeconds: 100
});