Convert JS New() To Brython ? #2289
Answered
by
ed2050
BobSquarePants
asked this question in
Q&A
Replies: 2 comments 2 replies
-
Hi @BobSquarePants, yes just do window.DOMParser.new () to create the parser object, then call whatever methods you want on it. I use this helper function in my brython code: def make_html (text) :
'Turn a string into html nodes'
# see https://stackoverflow.com/questions/10585029/parse-an-html-string-with-js
parser = window.DOMParser.new ()
html = parser.parseFromString (text, 'text/html')
return html.body.firstElementChild This works for most cases. However I recently noticed that it fails if text contains a header element like:
Anything that gets put in html.head instead of html.body will fail. I don't use it for header nodes so I haven't gotten around to fixing it yet. |
Beta Was this translation helpful? Give feedback.
1 reply
-
This code:
window.new(Event('change'))
makes a new window object. To create an Event object, do this instead:
window.Event.new('change')
I didn't check the rest of your code, but that will certainly fix one
problem.
…On Sat, Jan 27, 2024 at 2:29 PM BobSquarePants ***@***.***> wrote:
Thank you @ed2050 <https://github.com/ed2050> , I didn't try your
solution yet, but I'll keep it warm when I will need it again.
I still need the new() function but something else
I'm binding an event to an element (input) and assign a function
def FooBar(ev):
print(ev.target.id)
ele.bind('change', FooBar)
I would like to launch Foobar once before even a change event occur
Apparently with JS it will look like
element.dispatchEvent(new Event('change'));//source: https://stackoverflow.com/questions/35659430/how-do-i-programmatically-trigger-an-input-event-without-jquery
I've try several way (based on your reply) but I can't make it work in
Brython
ele.dispatchEvent(window.new(Event('change')))#give error uncaught __dir__
—
Reply to this email directly, view it on GitHub
<#2289 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AIKNAODXSGB4A6DHQJAXF7TYQUFK7AVCNFSM6AAAAAA6PBG5D6VHI2DSMVQWIX3LMV43SRDJONRXK43TNFXW4Q3PNVWWK3TUHM4DENRVGUYDM>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
BobSquarePants
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi,
On the Brython documention page about JSobject
I see that Brython support a way to "translate" the JS new().
I would like to convert this
To Byrhton
I've tried
Any ideas ?
Beta Was this translation helpful? Give feedback.
All reactions