[JavaScript] Passing string to function

Discuss other programming languages besides AutoHotkey
User avatar
Scr1pter
Posts: 1272
Joined: 06 Aug 2017, 08:21
Location: Germany

[JavaScript] Passing string to function

28 Mar 2020, 08:09

Hello,

I need to work with JavaScript to create scripts for some musical program.
After some days of researches I've made some good progress.

Right now I'm having problems to pass a string to a function as parameter, though.

Code: Select all

const kPackageID = "My first test script"; // package ID (must be unique)

function testAction(actionValue)
{
  this.interfaces = [Host.Interfaces.IEditTask, Host.Interfaces.IParamObserver] //, Host.Interfaces.IObserver] 
  this.prepareEdit = function (context)
  {
    return Host.Results.kResultOk;
  }  
  this.performEdit = function (context)
  {        
    switch (actionValue)
    {
      case "testA":
        testA();
        break;
      case "testB":     
        testB("Test");
        break;
    }
  }
}
function testA() 
{
  var stringVar = "A simple test"
  Host.GUI.alert("My message is: "+stringVar); // Result = My message is: A simple test
  return
}
function testB(value) 
{
  Host.GUI.alert("My other message is: "+value); // Result = My other message is: [object Native_00000000003F0000_ScriptUplink]
  return
}
How can I achieve a correct behaviour in the 2nd function (testB)?
I already tried to call it with testB('Test');, but the result is the same strange one.

Thanks for any help and best regards!
Please use [code][/code] when posting code!
Keyboard: Logitech G PRO - Mouse: Logitech G502 LS - OS: Windows 10 Pro 64 Bit - AHK version: 1.1.33.09
iamkajal
Posts: 4
Joined: 10 Jun 2021, 21:38

Re: [JavaScript] Passing string to function

22 Jun 2021, 02:32

the argument which is a string in the onClick attribute of the button which calls a function with a string as an argument using onClick() method.
You can try this code.

Code: Select all

<!DOCTYPE HTML>
<html>
  
<head>
    <title>
        JavaScript 
      | Pass string parameter in onClick function.
    </title>
</head>
  
<body style="text-align:center;" 
      id="body">
    <h1 style="color:green;">  
            GeeksForGeeks  
        </h1>
    <p id="GFG_UP" 
       style="font-size: 19px; 
              font-weight: bold;">
    </p>
    <button onclick="GFG_Fun('Parameter'); ">
        click here
    </button>
    <p id="GFG_DOWN"
       style="color: green; 
              font-size: 24px;
              font-weight: bold;">
    </p>
    <script>
        var up = 
            document.getElementById('GFG_UP');
        var down = 
            document.getElementById('GFG_DOWN');
        up.innerHTML = 
          'Click on button to pass the '+
          'string parameter to the function';
  
        function GFG_Fun(parameter) {
            down.innerHTML = "String '" 
            + parameter + "' Received";
        }
    </script>
</body>
  
</html>
blaarkies
Posts: 2
Joined: 04 Nov 2021, 05:59
Contact:

Re: [JavaScript] Passing string to function

04 Nov 2021, 06:49

Scr1pter wrote:
28 Mar 2020, 08:09
Host.GUI.alert("My other message is: "+value); // Result = My other message is: [object Native_00000000003F0000_ScriptUplink]
Looking at the result, it seems that the `value` is an object. The `.alert()` function call probably expects a string, and when given an object instead, javascript will try to make a string out of that
- If `value` object has a `.toString()` method in it, javascript will use that to make a string value
- If not, javascript will say the string is `[object Object]`

With that being said, i have never seen the output like `[object Native_00000000003F0000_ScriptUplink]`, I am not sure what type of object that is.

Do you have more information on what type of program/interface is running?

Return to “Other Programming Languages”

Who is online

Users browsing this forum: No registered users and 44 guests