question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

inner_html containing script tag breaks page

See original GitHub issue

The following example should insert a script tag into a div:

import justpy as jp

wp = jp.WebPage()
d = jp.Div()
d.inner_html = '<script>alert("Hello World!");</script>'
wp.add(d)

jp.justpy(lambda: wp)

But the result is a page full of JavaScript code. It looks like the closing </script> causes the the remaining code of the JustPy framework to be interpreted as HTML.

Although I’m aware of other possibilities to insert code into the HTML body (e.g. body_html), this smells like unsafe behavior. Besides, it would be handy to be able to just insert any HTML via inner_html.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:9

github_iconTop GitHub Comments

1reaction
elimintzcommented, Aug 29, 2022

This is interesting: https://www.danielcrabtree.com/blog/25/gotchas-with-dynamically-adding-script-tags-to-html Apparently htnl5 does not execute scripts inserted via innerHtml

Perhaps the solution is to wait for page_ready and then run a JavaScript script that inserts the wanted script not using inner html.

0reactions
WolfgangFahlcommented, Aug 30, 2022

See #473 and #471 - i suggest we allow user specific java code to be explicitly loaded as part of the startup process. That will be way cleaner than having to fiddle with html as such. Especially debugging the java script code and setting break points will be way easier.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can scripts be inserted with innerHTML? - Stack Overflow
Note: script elements inserted using innerHTML do not execute when they are inserted. But beware, this doesn't mean innerHTML is safe from cross-site...
Read more >
Run script tags in innerHTML content - Ionuț Colceriu
When inserting HTML content in the DOM using innerHTML, script tags inside it will not load or run. This applies to both inline...
Read more >
JavaScript HTML DOM - Changing HTML - W3Schools
The HTML DOM allows JavaScript to change the content of HTML elements. ... DOM to get the element with id="p1"; A JavaScript changes...
Read more >
The Curious Case of document.write - Eager Blog
document.write is weird. It inserts HTML into the page wherever it appears. Well, it inserts its HTML directly after the script tag it...
Read more >
Element.innerHTML - Web APIs | MDN
The innerHTML property can be used to examine the current HTML source of the page, including any changes that have been made since...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found