Splunk Search

How to use external variables in search command?

kjycls
Engager

application.js
value = Splunk.util.getParameter("name");
localStorage.setItem("name",value);

I saved parameter value by using html5 localStorage.(similar session value control)

in application.js file, I can get name value -> localStorage.getItem("name")

in this case, I want use this value to splunk advancedXML search variable.

How can I do??

Tags (3)
0 Karma

sideview
SplunkTrust
SplunkTrust

You could use a NullModule and then write a tricky patch from application.js, or you could make life a bit easier and use Sideview Utils and its "CustomBehavior" module.

Step 1) download Sideview Utils 2.1.X from http://sideviewapps.com/apps/sideview-utils

Step 2) put the SideviewUtils module on your view. Put it at the top next to AccountBar and AppBar. It's invisible and it'll be out of the way up there.
ie:

<module name="AccountBar" layoutPanel="appHeader" />
<module name="AppBar" layoutPanel="appHeader" />
<module name="SideviewUtils" layoutPanel="appHeader" />

Step 3) Put a CustomBehavior module into the page, at a point in the Advanced XML downstream from which you want the value to be available. Given it a "customBehavior" param of "getNameFromLocalStorage"

ie:

<module name="CustomBehavior">
  <param name="customBehavior">getNameFromLocalStorage</param>

  <module name="HTML">
    <param name="html"><![CDATA[
      The name from localStorage is $name$
    ]]></param>
  </module>      
</module>

Step 4) in application.js, you define the customBehavior like so.

Sideview.utils.declareCustomBehavior("getNameFromLocalStorage", function(customBehaviorModule) {
    customBehaviorModule.getModifiedContext = function() {
        var context = this.getContext();
        context.set("name", localStorage.getItem("name"));
        return context;
    }
});

Step 5) downstream from the CustomBehavior module you can use the Sideview Search module or HTML module to reference $name$, and at runtime it will be the value from localStorage. (See the example I listed in Step 3, above)

If you want to use the trickier way with the NullModule patch, it's not that bad in this case, and I can spell that out for you too..

0 Karma

sideview
SplunkTrust
SplunkTrust

I added more code samples to walk you through it, and I also fixed an embarassing typo in the customBehavior javascript. Sorry about that.

0 Karma

kjycls
Engager

Thank you~ Can I know Stop3 syntax?

0 Karma
Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.
Get Updates on the Splunk Community!

Observe and Secure All Apps with Splunk

 Join Us for Our Next Tech Talk: Observe and Secure All Apps with SplunkAs organizations continue to innovate ...

What's New in Splunk Observability - August 2025

What's New We are excited to announce the latest enhancements to Splunk Observability Cloud as well as what is ...

Introduction to Splunk AI

How are you using AI in Splunk? Whether you see AI as a threat or opportunity, AI is here to stay. Lucky for ...