Example: Basic page using JavaScript for apps outside Splunk Web

This example shows how to use JavaScript to create a basic page with a chart and an events viewer that displays results from a search.

To use this code, save it as an HTML file in your own web site's directory. Be sure to update the splunkjs.config section for your own web app. The authenticate function redirects to a login_form.html example login page, which you can find here. For more, see Use SplunkJS Stack in your own web apps.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title>Basic Page</title>

    <link rel="stylesheet" type="text/css" href="static/splunkjs/css/bootstrap.css" />
</head>


<body>
    <h4>A chart and an events viewer displaying results of a search</h4>
    <div id="mychart"></div>
    <div id="myeventsviewer"></div>

    <script src="static/splunkjs/config.js"></script>

    <script>
        // Configure SplunkJS Stack
        splunkjs.config({
            proxyPath: "/proxy",
            scheme: "https",
            host: "localhost",
            port: 8089,
            authenticate: function (done) {
                require([
                    "jquery",
                    "jquery.cookie"
                ], function ($) {
                    // Retrieve the session key and username from cookies
                    var splunkSessionKey = $.cookie("splunk_sessionkey");
                    var splunkCurrentUser = $.cookie("splunk_username");

                    // Log in using the session key and username
                    if (splunkSessionKey) {
                        done(null, {
                            sessionKey: splunkSessionKey,
                            username: splunkCurrentUser
                        });
                    }
                    // If there is no session key, redirect to the login form
                    else {
                        window.location.replace("login_form.html");
                    }
                });
            }
        });

        // Configure the web site's base URL
        require.config({
            baseUrl: "static/"
        });

        // Set up the Web Framework components
        var deps = [
            "splunkjs/ready!",
            "splunkjs/mvc/searchmanager",
            "splunkjs/mvc/chartview",
            "splunkjs/mvc/eventsviewerview"
        ];
        require(deps, function (mvc) {
            // Load individual components
            var SearchManager = require("splunkjs/mvc/searchmanager");
            var ChartView = require("splunkjs/mvc/chartview");
            var EventsViewerView = require("splunkjs/mvc/eventsviewerview");

            // Instantiate the views and search manager
            var mysearch = new SearchManager({
                id: "search1",
                preview: true,
                cache: true,
                status_buckets: 300,
                search: "index=_internal | head 1000 | stats count by sourcetype"
            });

            var mychart = new ChartView({
                id: "chart1",
                managerid: "search1",
                type: "bar",
                el: $("#mychart")
            }).render();

            var myeventsviewer = new EventsViewerView({
                id: "eviewer1",
                managerid: "search1",
                el: $("#myeventsviewer")
            }).render();
        });
    </script>
</body>
</html>