I am trying to disable the JavaScript when the user is using IE. Is there a way to accomplish this in template.php or a custom module?
-
2@Pekka: To punish them. That'll learn 'em.George Cummins– George Cummins2011-06-21 15:38:40 +00:00Commented Jun 21, 2011 at 15:38
-
2@George but there are so much more effective ways to do that! Redirect them to rotten.com or lemonparty, for example. Or run the earthquake effect (still works in IE, yay!)Pekka– Pekka2011-06-21 15:43:08 +00:00Commented Jun 21, 2011 at 15:43
-
1seriously though, if you want to disable JS (which is not really possible) because of a compatibility problem, it is more feasible to try and fix that problem. It's often easy.Pekka– Pekka2011-06-21 15:43:51 +00:00Commented Jun 21, 2011 at 15:43
-
@Pekka: The problem is with a module called map hilight. It works well on all other browsers except you know who. I investigated a fix but could not find a mystory patch that supposedly will deal with the issues. Right now, my reasoning is I will just remove a small piece of functionality so my client can atleast use their site hence my reason for trying to diable/remove the script when IE is detected.sisko– sisko2011-06-21 16:02:50 +00:00Commented Jun 21, 2011 at 16:02
2 Answers
As alternative to handling the content of $vars['scripts'], which is a string containing the HTML to output in the <head> tag, you could use the value returned from drupal_add_js(NULL, NULL, 'header'), which is similar to the following one:
$header_javascript = array(
'core' => array(
'misc/jquery.js' => array(
'cache' => TRUE,
'defer' => FALSE,
'preprocess' => TRUE,
),
'misc/drupal.js' => array(
'cache' => TRUE,
'defer' => FALSE,
'preprocess' => TRUE,
),
),
'module' => array(),
'theme' => array(),
'setting' => array(
array('basePath' => base_path()),
),
'inline' => array(),
);
The "module" index contains the reference to the JavaScript files added from the modules, "settings" contains all the JavaScript settings generally added by the modules, and "inline" contains inline JavaScript code.
It could help if you need to distinguish between the different JavaScript files, and (for example) not touch any JavaScript file that has been marked as "core."
The counterbalance is that to populate the content of $vars['scripts'] you need to duplicate part of the code used from drupal_get_js(). As you would need a customized code, you would not duplicate all the code of that function, but you still would duplicate part of it.
In Drupal 7, the variable $vars['scripts'] is not passed anymore to template_preprocess_page() (and similar preprocess functions implemented by modules or themes); it is passed to template_preprocess_html().
Comments
You can use the preprocess_page() hook in template.php.
function YOUR_THEME_preprocess_page(&$vars) {
if (isset($_SERVER['HTTP_USER_AGENT']) && (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false)) {
$vars['scripts'] = 'get a mac!';
}
}
Obviously you should do something more intelligent with the $vars['scripts'] content :)
5 Comments
if (navigator.userAgent.match(/MSIE/i) != null) {...} else {...}