Dashboards & Visualizations

Javascript stopped working in 9.1.1

timgren
Path Finder

Custom token script stopped working. Can anyone spot any obvious errors? It worked perfectly from version 6.x - 8.x 

I the error "A custom JavaScript error caused an issue loading your dashboard. See the developer console for more details." The console isnt very helpful. 

common.js:1702 Error: Script error for: util/console
http://requirejs.org/docs/errors.html#scripterror
at makeError (eval at e.exports (common.js:1:1), <anonymous>:166:17)
at HTMLScriptElement.onScriptError (eval at e.exports (common.js:1:1), <anonymous>:1689:36)


// Tokenize.js 

require(['jquery', 'underscore', 'splunkjs/mvc', 'util/console'], function($, _, mvc, console) {
     function setToken(name, value) {
         var defaultTokenModel = mvc.Components.get('default');
         if (defaultTokenModel) {
             defaultTokenModel.set(name, value);
         }
         var submittedTokenModel = mvc.Components.get('submitted');
         if (submittedTokenModel) {
             submittedTokenModel.set(name, value);
         }
     }

// Main

     $('.dashboard-body').on('click', '[data-on-class],[data-off-class],[data-set-token],[data-unset-token],[data-token-json]', function(e) {
         e.preventDefault();

console.log("Inside the click");
         var target = $(e.currentTarget);
console.log("here");
console.log("target.data('on-class')=" + target.data('on-class'));
 
var cssOnClass= target.data('on-class');
var cssOffClass = target.data('off-class');

if (cssOnClass) {
$("." + cssOnClass).attr('class', cssOffClass);
target.attr('class', cssOnClass);
}
         var setTokenName = target.data('set-token');
         if (setTokenName) {
             setToken(setTokenName, target.data('value'));
         }
         var unsetTokenName = target.data('unset-token');
         if (unsetTokenName) {
    var tokens = unsetTokenName.split(",");
var arrayLength = tokens.length;
for (var i = 0; i < arrayLength; i++) {
   setToken(tokens[i], undefined);
    //Do something
}      
             //setToken(unsetTokenName, undefined);
         }
         var tokenJson = target.data('token-json');
         if (tokenJson) {
             try {
                 if (_.isObject(tokenJson)) {
                     _(tokenJson).each(function(value, key) {
                         if (value == null) {
                             // Unset the token
                             setToken(key, undefined);
                         } else {
                             setToken(key, value);
                         }
                     });
                 }
             } catch (e) {
                 console.warn('Cannot parse token JSON: ', e);
             }
         }
     });
 });

 

Labels (1)
0 Karma

kamlesh_vaghela
SplunkTrust
SplunkTrust

@timgren 

Just remove  util/console  and console form the require block.

like

require(['jquery', 'underscore', 'splunkjs/mvc'], function($, _, mvc) {

 

You can directly use console  object in your JS code.

 

Full code.

require(['jquery', 'underscore', 'splunkjs/mvc'], function($, _, mvc) {
    console.log("hieeeee");
    function setToken(name, value) {
        var defaultTokenModel = mvc.Components.get('default');
        if (defaultTokenModel) {
            defaultTokenModel.set(name, value);
        }
        var submittedTokenModel = mvc.Components.get('submitted');
        if (submittedTokenModel) {
            submittedTokenModel.set(name, value);
        }
    }

// Main

    $('.dashboard-body').on('click', '[data-on-class],[data-off-class],[data-set-token],[data-unset-token],[data-token-json]', function(e) {
        e.preventDefault();

console.log("Inside the click");
        var target = $(e.currentTarget);
console.log("here");
console.log("target.data('on-class')=" + target.data('on-class'));

var cssOnClass= target.data('on-class');
var cssOffClass = target.data('off-class');

if (cssOnClass) {
$("." + cssOnClass).attr('class', cssOffClass);
target.attr('class', cssOnClass);
}
        var setTokenName = target.data('set-token');
        if (setTokenName) {
            setToken(setTokenName, target.data('value'));
        }
        var unsetTokenName = target.data('unset-token');
        if (unsetTokenName) {
   var tokens = unsetTokenName.split(",");
var arrayLength = tokens.length;
for (var i = 0; i < arrayLength; i++) {
  setToken(tokens[i], undefined);
   //Do something
}      
            //setToken(unsetTokenName, undefined);
        }
        var tokenJson = target.data('token-json');
        if (tokenJson) {
            try {
                if (_.isObject(tokenJson)) {
                    _(tokenJson).each(function(value, key) {
                        if (value == null) {
                            // Unset the token
                            setToken(key, undefined);
                        } else {
                            setToken(key, value);
                        }
                    });
                }
            } catch (e) {
                console.warn('Cannot parse token JSON: ', e);
            }
        }
    });
});

I hope this will help you.

Thanks
KV
An upvote would be appreciated if any of my replies help you solve the problem or gain knowledge.

0 Karma
Get Updates on the Splunk Community!

Index This | Why did the turkey cross the road?

November 2025 Edition  Hayyy Splunk Education Enthusiasts and the Eternally Curious!   We’re back with this ...

Enter the Agentic Era with Splunk AI Assistant for SPL 1.4

  &#x1f680; Your data just got a serious AI upgrade — are you ready? Say hello to the Agentic Era with the ...

Feel the Splunk Love: Real Stories from Real Customers

Hello Splunk Community,    What’s the best part of hearing how our customers use Splunk? Easy: the positive ...