Not being able to render HTML inside the javascript action

0
Below, is my javascript code to make a url clickable but mendix is executing it as plain text. How do i fix it?   // This file was generated by Mendix Studio Pro. // // WARNING: Only the following code will be retained when actions are regenerated: // - the import list // - the code between BEGIN USER CODE and END USER CODE // - the code between BEGIN EXTRA CODE and END EXTRA CODE // Other code you write will be lost the next time you deploy the project. import "mx-global"; import { Big } from "big.js";   // BEGIN EXTRA CODE // END EXTRA CODE   /** * @param {string} wholeMessage * @returns {Promise.<string>} */ export async function ChatMessageWithLink(wholeMessage) { // BEGIN USER CODE   function convertTextToLinks(text) { // Robust URL pattern const urlPattern = /https?:\/\/[^\s<>"']+/g; return text.replace(urlPattern, function(url) { return `<a href="${url}" target="_blank">${url}</a>`; }); }   const messageWithLinks = convertTextToLinks(wholeMessage); return messageWithLinks;   // END USER CODE }  
asked
3 answers
0

Hi puja,

In your js action you are returning html string which you have to pass to widget that can handle html string , normal text label will treat this as plain text

ck editor you can download this ck editor module ,it comes with ck editor viewer , just pass your string to viewer, it will render as button.

Also for your requirement you don't need JavaScript to create button.

You can use a link button and give your url there directly.

 

Hope it helps!

answered
0

try this and set return type of javascript action as nothing

// This file was generated by Mendix Studio Pro.
//
// WARNING: Only the following code will be retained when actions are regenerated:
// - the import list
// - the code between BEGIN USER CODE and END USER CODE
// - the code between BEGIN EXTRA CODE and END EXTRA CODE
// Other code you write will be lost the next time you deploy the project.

import "mx-global";
import { Big } from "big.js";

// BEGIN EXTRA CODE
// END EXTRA CODE

/**
 * @param {string} wholeMessage
 * @returns {Promise.<void>}
 */
export async function ChatMessageWithLink(wholeMessage) {
    // BEGIN USER CODE

   
    const button = document.createElement('button');

    button.innerText = wholeMessage;

    button.addEventListener('click', function() {
        window.open(wholeMessage, '_blank');
    });

    
    // appending to the body
    document.body.appendChild(button);

    // END USER CODE
}

 

answered
0

Hi Puja,

I have tried my code it works fine see below

image.png

image.png

answered