custom js in bootstrap sub-theme is loaded, works in console, but doesn’t actually work in browser

I have a bootstrap subtheme with scripts[] = script.js in the .info file. When I go to the site and view html source of the page I can see that drupal is including the file <script src="https://site/path/to/subtheme/script.js?ox2bif"></script> and if I click on that link and I see the expected contents of the file:

jQuery(document).ready(function(){   jQuery('p').click(function(){alert("hello");}); }); 

The js works, the links to the actual file. What else is there?

If I paste jQuery('p').click(function(){alert("hello");}); into my browser’s js console then, obviously, clicking any paragraph opens an alert popup window. But what am I missing that it’s not working in the Drupal theme?


Additional info:

  • Drupal 7
  • Bootstrap base theme 7.x-3.15
  • Using Bootstrap CDN (not the SASS or LESS starterkits) with version 3.3.7
  • Using jQuery version 2.1
    • switching to jQuery 2.2 or 3.1 doesn’t not help

Currently doing:

function winprevent_custom_page_build(&$page) {                                                     $themescript = drupal_get_path('theme', 'winprevent') . '/script.js';   drupal_add_js($themescript, array('group' => JS_LIBRARY, 'weight' => -10)); 

…in a custom module to load the script from the theme. Notice the 'group' => JS_LIBRARY; this was necessary to get the file to load before admin_menu module’s js. Disabling admin_menu stops it from breaking. The presence of admin_menu.js also breaks the animation of Bootstrap’s collapse feature.

So scripts[] = script.js actually does work in the theme but only for anonymous users, it breaks for the authenticated users (who all have access to the admin menu).

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

custom js in bootstrap sub-theme is loaded, works in console, but doesn’t actually work in browser

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.