https://github.com/perry-mitchell/shake-n-bake
Execute script elements that are injected through HTML
https://github.com/perry-mitchell/shake-n-bake
Last synced: 9 months ago
JSON representation
Execute script elements that are injected through HTML
- Host: GitHub
- URL: https://github.com/perry-mitchell/shake-n-bake
- Owner: perry-mitchell
- License: mit
- Created: 2016-01-13T06:13:22.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2016-01-13T14:48:43.000Z (over 10 years ago)
- Last Synced: 2025-01-03T13:23:46.997Z (over 1 year ago)
- Language: JavaScript
- Size: 4.88 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Shake-N-Bake
Execute script elements that are injected through HTML.
[](https://travis-ci.org/perry-mitchell/shake-n-bake)
## Why
When injecting HTML into an element using `element.innerHTML = "..."`, scripts contained within are not executed. For example, the following JavaScript within the quoted HTML will not execute:
```
var container = document.getElementById("container");
container.innerHTML = '
alert("This won't appear");';
```
## How it works
SnB simply takes the content of the script out of the element, and creates a new script element with the contents. It inserts it back into the DOM as an element instance (not a string) and the browser executes the contents.