Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

wonder95's avatar

How load external tokenizer script when modal is opened

I am doing online payments using a HeadlessUI Dialog component, and to load the external tokenizer js that generates the token I pass to the processor when submitting a payment, I have the following code

The issue I have is that when the page that has the button that triggers the model is first loaded, the onMounted hook runs, and the first time you click the button to open this modal, the credit card fields are displayed fine. However, if I close the model and re-open it without reloading the parent page, the cc fields are not displayed, and I get a console error that Uncaught Error: Could not find container from the tokenizer.js file. If I refresh the parent page, it works fine. My thought was to use a different event that would fire when the dialog opens, but looking at the Vue component lifecycle and the Dialog docs, I'm not seeing one. How can I make it so that the external tokenizer script can always find the container when it is opened without having to reload the parent page?

0 likes
1 reply
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

To ensure that the external tokenizer script can always find the container when the modal is opened, you need to make sure that the script is loaded and the tokenizer is initialized every time the modal is opened. Instead of relying solely on the onMounted lifecycle hook, you can use a combination of Vue's watch function to detect when the modal is opened and then load the script and initialize the tokenizer.

Here's a revised version of your code that uses a watch function to detect when the modal is opened and then loads the tokenizer script and initializes the tokenizer:

Explanation:

  1. Props and Emits: The isOpen prop is used to detect when the modal is opened.
  2. Tokenizer Initialization: The doTokenize function initializes the tokenizer.
  3. Script Loading: The loadTokenizerScript function loads the tokenizer script and initializes the tokenizer once the script is loaded.
  4. Watch Function: The watch function monitors the isOpen prop. When the modal is opened (isOpen becomes true), it calls loadTokenizerScript to load the script and initialize the tokenizer.

This approach ensures that the tokenizer script is loaded and the tokenizer is initialized every time the modal is opened, preventing the issue where the container is not found.

1 like

Please or to participate in this conversation.