Allows loading html, including scripts - similar to jQuery's ".html()"
https://www.danielcrabtree.com/blog/25/gotchas-with-dynamically-adding-script-tags-to-html
and
https://stackoverflow.com/questions/8097026/how-to-run-javascript-in-html-loaded-via-ajax/8097325#8097325
used as references - thanks guys!
Kind: global class
Author:: footjohnson
- loadScriptAsync
- .ReplaceHtml(html, domLocation) ⇒
boolean
- .CheckForScripts(node) ⇒
boolean
- .PlaceElems(element, domLocation, checkScripts, isHead) ⇒
boolean
- .ReplaceHtml(html, domLocation) ⇒
Replaces html on page with given html, executing scripts in the process.
Kind: instance method of loadScriptAsync
Returns: boolean
- True if success; false if failure
Param | Type | Description |
---|---|---|
html | string |
Html to replace current html |
domLocation | HTMLElement |
Node whose html should be replaced |
Example
// html should be stringified HTML
// domLocation would be given via something like document.getElementById("elementId");
// returns true
loadScriptAsync.ReplaceHtml(html, domLocation)
Checks if there are any script tags in the node
Kind: instance method of loadScriptAsync
Returns: boolean
- True if it contains scripts; false if it doesn't
Param | Type | Description |
---|---|---|
node | Node |
Node to check for scripts |
Example
// Returns whether a node or HTMLElement has script tags as children or grandchildren
// returns true
loadScriptAsync.CheckForScripts(node)
Recursively places html elements on the DOM
Kind: instance method of loadScriptAsync
Returns: boolean
- True if success; false if failure
Param | Type | Default | Description |
---|---|---|---|
element | HTMLElement |
Element whose children should be placed on the DOM | |
domLocation | Node |
Where to put the elements | |
checkScripts | boolean |
true |
If the function should first check if any scripts tags are within the given elems. If there are, use innerHTML instead |
isHead | boolean |
false |
If html should be placed in the head of the document (not yet supported) |
Example
// returns true
loadScriptAsync.PlaceElems()