2

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?

4
  • 2
    @Pekka: To punish them. That'll learn 'em. Commented 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!) Commented Jun 21, 2011 at 15:43
  • 1
    seriously 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. Commented 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. Commented Jun 21, 2011 at 16:02

2 Answers 2

1

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().

Sign up to request clarification or add additional context in comments.

Comments

0

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

Great suggestion. Operation 'Get a mac!' would have worked but $var['scripts'] returns a long string of .js files. Unfortunately, this means I can't unset the undesired javascript file. Thanks for the suggestion however.
@sisko, you will need to parse that string, remove what you want, and then set the $vars['scripts'] with the remainder.
@sisko: perhaps its better to do the browerser sniffing in the javascript file, and provide different javascript for ie6?
I was trying to avoid that because I didn't want to hack code I barely understand. And I couldn't think of a way to do the sniffing and diverting from my own javascript code. And, it's all versions of IE - not just 6 ... typical :-)
Javascript sniffing and diverting for IE is easy: if (navigator.userAgent.match(/MSIE/i) != null) {...} else {...}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.