Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: Elem.button - onClick? #62

Closed
JanBizub opened this issue Feb 10, 2022 · 3 comments
Closed

Question: Elem.button - onClick? #62

JanBizub opened this issue Feb 10, 2022 · 3 comments

Comments

@JanBizub
Copy link

JanBizub commented Feb 10, 2022

Hello,

how do I assign function to button's onClick in Falco?

I am attempting something like: let button = Elem.button [ Attr.onClick (fun _ -> xxxx) ] [ ]

but it does not exist. I was looking into Falco source code for something like onClick but I have not found anything.

@pimbrouwers
Copy link
Owner

pimbrouwers commented Feb 10, 2022

Hi Jan,

Thanks for reaching out.

As you have discovered, the "onClick" attribute doesn't exist in the Attr module. I omitted the JavaScript attributes because the module is easily extended on a per-project basis, and because it is generally considered a poor security practice to allow for the execution of inline JavaScript with your CSP.

Before I leave some example code, I just want to point out that the F# lamba as you have it will not work. The Markup module produces literal HTML, thus the XmlAttribute type expects a string for both the name and value of the attribute. In the case of onclick, the expecation for the value is a string literal that contains executable JavaScript.

module Attr = 
    let onclick v = Attr.create "onclick" v

Elem.button [ Attr.onclick "(function() { console.log('here') })()" ] [ Text.raw "Click" ]
// Equivalent to: <button onclick="(function() { console.log('here') })()">Click</button>

Let me know if that makes sense, or if you have any other questions.

Cheers

@JanBizub
Copy link
Author

Hello Pim, thank you for reply. I was aware that lambda wont work it was just for illustration. It is reasonable. If I wanted javascript onClick then I add an id to the element using Attr.id reference .js file and assign onClick event there. What I was originally after was a button that redirects user, but I used Elem.a masked by bootstrap as button with href. I like Falco very much so far thank you for creating it. :)

@pimbrouwers
Copy link
Owner

Appreciate that! Glad you enjoy it! Tell your friends!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants