Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
When aggregating our JS, we hit errors on panels admin pages. Example:
Error: Uncaught ReferenceError: id is not defined
From this:
for (id in Drupal.settings.CTools.dependent) {
The problem is that id has not been defined. By itself, this script will work fine, but we have enabled aggregation, and another script must be setting strict mode, as this line throws an error in such a case due to the variable not having been declared.
The solution is to declare variables in JS before using them:
for (var id in Drupal.settings.CTools.dependent) {
I will apply a patch to the first comment.
Comment | File | Size | Author |
---|---|---|---|
#6 | 3063176-5.patch | 3.66 KB | joelpittet |
#3 | ctools-javascript_undeclared_variables-3063176-2.patch | 1.32 KB | Jaypan |
#2 | ctools-javascript_undeclared_variables-3063176-1.patch | 1.48 KB | Jaypan |
Comments
Comment #2
Jaypan CreditAttribution: Jaypan commentedAttaching patch.
Comment #3
Jaypan CreditAttribution: Jaypan commentedOops, bad file references in patch. Uploading new patch.
Comment #4
joelpittetThanks @Jaypan, I've committed this to the dev branch to be included in the next release.
Here's a documentation page for reference https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statem...
Comment #5
Jaypan CreditAttribution: Jaypan commentedGracias :)
Comment #6
joelpittetActually before I committed I found some other instances and it doesn't relate to the documentation but these others are the same as the patch.
Could you double check I got them all?
Comment #7
joelpittetI used this regex to find the others
for ?\(.* in
Comment #8
Jaypan CreditAttribution: Jaypan commentedTo be honest, I always run my scripts through JSlint. It's a hassle to learn all their rules, but once you do, it makes for much cleaner JavaScript, that is syntaxically cross-browser friendly, in strict mode. Of course, if you make a call to a function one browser supports but another doesn't, that won't work.
For example, I cleaned up dependent.js like this:
This passes JSlint. Note it's not tested.
Comment #9
joelpittetI'm confident the changes we made in the patch won't break anything and are easy to commit with minimal review time. To keep the scope low on this but get all the bits in one shot could you do a quick pass to see if the changes look good from your end?
Comment #10
joelpittetLinters are great but better to start with from the beginning, we are using eslint in D8 core.
Comment #11
Jaypan CreditAttribution: Jaypan commentedWell, I run JSlint just locally, before committing any code to DO. It's something I do separately.
I gave the patch from #6 a go-over. There is nothing that could break anything in there.
Comment #12
Jaypan CreditAttribution: Jaypan commentedComment #14
joelpittetThanks for having a look over it. I've committed it to the dev branch (for real). That's a good practice to get into to run a linter over everything. I think the testbot people (@mixologic and company) are looking to do that with CSS and JS for all drupal projects.
Comment #16
Chris Matthews CreditAttribution: Chris Matthews as a volunteer commented