How to load a custom jQuery version for a specific Drupal page

Recently, I worked through an IE8 / Drupal / jQuery nightmare. jQuery 1.2.x which currently ships with Drupal breaks IE8 (thanks, Microsoft). So I installed the jquery_update module, which updates jQuery so it doesn't break IE8. But then some scripts I had written broke as well. I ended up only getting those scripts to work with jQuery 1.4.x, but of course 1.4.x breaks other things on the site (like itweak_upload's draggable attachments).

The long and short of it is that I needed a way to load 1.4.x on just one page, and load 1.2.x on all other pages. If you're stuck with a similar problem, here's how you can fix it.

  1. Disable the jQuery update module if you have it enabled.
    If you have jQuery update installed, just replace /misc/jquery.js with the jquery file it is replacing. jQuery update interferes with the preprocess function we will be using.

  2. Insert this preprocess module into a theme or a function.
    <?php
    function yourModuleOrThemeName_preprocess_page(&$variables) {
      if(arg(0) == 'yourPage') {
        $scripts = drupal_add_js();
        
        $new_jquery = array('yourNewJQueryPath/jquery14.js' => $scripts['core']['misc/jquery.js']);
        $scripts['core'] = array_merge($new_jquery, $scripts['core']);
        unset($scripts['core']['misc/jquery.js']);
        
        $variables['scripts'] = drupal_get_js('header', $scripts);  
      }
    }
    ?>
    

Blam! This preprocess code will allow you to replace the core /misc/jquery.js for a specific page.