Categories

How to Call JavaScript Function with a Button Using Popupsmart

Popupsmart allows you to trigger JavaScript functions directly through your popup campaigns. This feature is perfect for integrating web tools, such as opening a chat widget or performing custom actions based on user interaction.

You can execute JavaScript functions using one of the supported formats. Here’s an example on how to integrate a chat widget:

calling a JS function using Popupsmart

Direct Function Call

1. Define the function on your website as shown in the example.

function openChatWidget() {
    console.log("Chat widget opened!");
}

2. In your Popupsmart dashboard, set the JavaScript call action to:

openChatWidget

Function via window Object

1. Define the function globally as shown in the example.

javascript
Copy the code
window.openChatWidget = function() {
    console.log("Chat widget opened via window object!");
    // Add your chat widget's specific function here
};

2. In your Popupsmart dashboard, set the JavaScript call action to:

window.openChatWidget()

Supported Formats

Popupsmart supports the following JavaScript call formats:

  • testFunc: Calls a function by its name.
    • Example: function testFunc() { console.log("Function called!"); }
  • window.testFunc: Calls a function defined as part of the global window object.
    • Example: window.testFunc = function() { console.log("Window function called!"); };
  • testFunc(): Calls a parameterless function directly.
    • Example: function testFunc() { alert("Hello!"); }
  • window.testFunc(): Calls a parameterless function via the global window object.
    • Example: window.testFunc = function() { alert("Window says hello!"); };
  • testFunc(param1, param2): Calls a function with parameters.
    • Example: function testFunc(param1, param2) { console.log(param1, param2); }
  • window.testFunc(param1, param2): Calls a function with parameters through the global window object.
    • Example: window.testFunc = function(param1, param2) { console.log(param1, param2); };

Still have questions? Do not hesitate to contact our support team via our AI chatbot.