Loading browser specific .js files using drupal_add_js

1 reply [Last post]
billp44ruby
Offline
Last seen: 6 years 9 weeks ago
Joined: 2011-12-02

I'm currently using the template.php file to load a page specific javascript file as part of my theme. Because some of the idiosyncrasies with how Internet Explorer v7 and v8 handle some jQuery issues, I have to add a different .js file for users visiting with those browsers. How would I incorporate that into the following code sample?

function themename_preprocess_page(&$variables) {

$path = drupal_get_path_alias($_GET['q']);

if ($path == 'page/pg1') {
drupal_add_js('/sites/all/themes/themename/js/pg1_jso.js');
}
if ($path == 'page/pg2') {
drupal_add_js('/sites/all/themes/themename/js/pg2_jso.js');
}
if ($path == 'page/pg3') {
drupal_add_js('/sites/all/themes/themename/js/pg3_jso.js');
}
}

Drave Robber
Offline
Last seen: 5 years 44 weeks ago
Joined: 2012-08-07
Generally, it goes along the

Generally, it goes along the lines of:

$user_agent = $_SERVER['HTTP_USER_AGENT'];
if (strpos($user_agent, 'MSIE 6.0')) {
  drupal_add_js('/sites/all/themes/mytheme/js/msie6mustdie.js');
}

This requires some knowledge of what common user-agent strings look like; to save time on research, you can download some decent theme that is known to handle different CSS/js for different browsers and search its code for HTTP_USER_AGENT.

Add to playlist

Add to , or

Add to new playlist:

Add to playlist
This is a member-only feature
But, it's easy to become a member

Add to cart:


Loading...
Contact us

Below are the supported keyboard shortcuts for our video player.

space
Start / stop video
shift
Go to next video
shift
Go to previous video
Increase or decrease player speed by 20%
Skip ahead or go back by 15 seconds
1...9
Jump to a percentage (2 is 20%, for example)
f
Toggle full screen
Videos Pricing More Search
BuildAModule Logo Icon BuildAModule Logo Icon
Become a member!
$29
1 month
$156
6 months
10% off
$276
12 months
20% off
All memberships come with the following:
24 / 7 access to the entire 1800+ video library
An innovative video player with a rolling transcript
Extensive Drupal and cross-platform training
A thorough and thoughtful training style
Progress tracking
Take notes on specific video sections
Intuitive multi-user management
How many users?
user
Go solo with a single user, or give multiple team members access through one account and get discounts by bundling multiple users into a single membership.