Use javascript in coldfusion

Hi

Can you tell me how to us javascript with Coldfusion?
By executing this javascript with div tag cfoutput the requested script was not working. Do i need to add something ?

<div><cfoutput>#Exp.Msg#</cfoutput></div>
															   
<script type="text/javascript> 
let element = document.querySelector('div')
element.innerText = element.innerText.replace(/(\(.*\)|\[.*\]) /,'').trim()
</script>

i need to remove the text in brackets which is in the results of #Exp.Msg#

or is there a better way to do it? any suggestion ?

Assuming this is the exact code you used, you’re missing a double quote at end of javascript in your script opening tag. However, why not just the replacement / removal server side using CF rereplace tag before cfoutputing it instead of adding an additional layer on the client side?

Hi thank you so much for reply. can you show me/example what you mean with “However, why not just the replacement / removal server side using CF rereplace tag before cfoutputing it instead of adding an additional layer on the client side?”

You can use coldfusion functions to replace a string, you dont need to do this with javascript in the browser:

<CFSET exception = ReReplace(Exp.Msg, "/(\(.*\)|\[.*\]) /","")>
<CFDUMP var="#exception#">

(untested, just as an example)

Thank you michael_diederich, i checked but it does not remove the text in the brackets. any other suggestion ?

Try this:

<CFSET Exp.Msg="This (here) is a [quick] test (to go)!">
<CFSET exception = ReReplace(Exp.Msg, "(\(.*?\)|\[.*?\])","","ALL")>
<CFDUMP var="#exception#">

Thx Andreas! It works as expected

1 Like