Skip to Main Content
Spotfire Ideas Portal
Status Already exists
Product Spotfire
Categories Scripting
Created by Guest
Created on Sep 13, 2022

Optional parameters for JavaScripts

I would like to apply default values when no script parameters are provided

  • Attach files
  • Guest
    Reply
    |
    Sep 28, 2022

    Perfect! It used to be a hassle to have to provide all the parameters before through the Insert JavaScript dialog. Now I can have a JavaScript with multiple parameters and leave them blank those without being forced to enter them.

  • Admin
    Magnus Rylander
    Reply
    |
    Sep 28, 2022

    Scripts in a text area are globally accessible in a way that your question implies. These scripts are wrapped inside an IIFE (https://developer.mozilla.org/en-US/docs/Glossary/IIFE) and is therefore run once and then disposed. If you want to create a global function as the one in the example below this can easily be done manually.

    This script snippet adds a global function multiply, with a configurable parameter 'defaultvalue' when the text area script is invoked. This will be accessible globally as it is stored on the global window object. Note that all JavaScript parameters are strings, hence the parseInt. These parameters are also added when the text area is being initially rendered.

    window.multiply = function(a, b = parseInt(defaultValue)) {
    return a * b;
    }
  • Guest
    Reply
    |
    Sep 22, 2022

    When creating a JavaScript on a text area and you want the user to provide some parameters, for example the background color or a masthead that is going to be used across many pages. If you add a JavaScript parameter, say bgColor and you use this script everywhere, then you need to add the background color on all scripts. Otherwise the script engine from Spotfire complains about a "required parameter". If no parameter is specified, then just take a default value.

    In other words, it would be nice to be able to implement the following code:

    function multiply(a, b = 1) {

    return a * b;

    }

    console.log(multiply(5, 2));

    // expected output: 10


    console.log(multiply(5));

    // expected output: 5



  • Admin
    Magnus Rylander
    Reply
    |
    Sep 21, 2022

    Please provide more details.