Getting Data In

Loading custom javascript in custom app

JacobPN
Path Finder

Hi all,

I created a custom app from the "sample app" template. I put a application.js file in the appserver/static folder.
This file contains:

console.log("hello")

However, when I open the app, the message is not displayed in the console. I have restarted splunk, cleared my browser cache, and used the _bump endpoint. It seems that the javascript just is not getting loaded. What am I missing here? (my Splunk version is 7.2.0)

Thanks!

Tags (1)
0 Karma

vnravikumar
Champion

Hi @JacobPN

It has to work, just cross check whether you had included javascript in your dashboard. The following code works for me. To include advanced js just follow the comment given by @sdchakraborty

<dashboard script="application.js">
  <label>sampledashboard</label>
</dashboard>

application.js

console.log("this is to test javascript file");
0 Karma

JacobPN
Path Finder

Hi @vnravikumar,

Thank you for your comment. Do you know a way to achieve this at the home page of the application? So outside of any dashboards?
Thanks!

0 Karma

sdchakraborty
Contributor

you js need to wrap inside require call. something like below,

require([
'jquery',
'underscore',
'splunkjs/mvc',
'splunkjs/mvc/simplexml/ready!'
],
function($,_,mvc){
    console.log("test")
}
)

then in dashboard form tag you need to include the js file name like below,

<form script="youjsfile.js">

Sid

0 Karma

JacobPN
Path Finder

Hi Sid,

Thanks for the help. Will this work in the home page of the appllcation as well? So not only in dashboards? I'm not sure were to reference the js-file in that case.
Thanks!

0 Karma

sdchakraborty
Contributor

Hi Jacob,

How you developed the application home page? Is it written in html? You can refer ths js in similar way in html page as well, using script tag.

http://web.simmons.edu/~grabiner/comm244/weeknine/including-javascript.html

Sid

0 Karma

JacobPN
Path Finder

Hi Sid,

I simply created a new app, from the sample app template. So:
"Manage apps" > "Create app" and then choose the template "sample_app" and click save.
Subsequently I placed the mentioned "applications.js" in the appserver/static folder of this app. There is no index.html or anything like that in that folder, where I could place the script tag.

Jacob

0 Karma

sdchakraborty
Contributor

Ahh I got it. To see how the java script works you need to create a dashboard in your app. There in the form tag you can add script.

https://docs.splunk.com/Documentation/Splunk/7.2.4/SearchTutorial/Createnewdashboard

And please ignore my previous comment as I dont want to confuse you. Follow whatever ma or @vnravikumar posted yesterday.

Sid

0 Karma

JacobPN
Path Finder

I tested it on a dashboard and that works. However, now I want it the work on the app home page. So I want it to log "hello" to the console, on the app homepage. Any ideas?

0 Karma
Get Updates on the Splunk Community!

Join Us for Splunk University and Get Your Bootcamp Game On!

If you know, you know! Splunk University is the vibe this summer so register today for bootcamps galore ...

.conf24 | Learning Tracks for Security, Observability, Platform, and Developers!

.conf24 is taking place at The Venetian in Las Vegas from June 11 - 14. Continue reading to learn about the ...

Announcing Scheduled Export GA for Dashboard Studio

We're excited to announce the general availability of Scheduled Export for Dashboard Studio. Starting in ...