I want to add a button, with an onclick event, to the page. It will eventually open an overlay div.
But, I can’t get even an alert running.
I’ve used a couple of techniques, like unsafeWindow, and adding the script tags to the body, but still doesn’t work.
This is the code that I’m using right now:
function main() {
var script_source = '<script type="text/javascript">function doenwedan(){alert("hebben we gedaan!");}</script>';
var link_knop = '<br /><br /><br /><br /><button onclick="doenwedan()" value="gaan we doen">gaan we doen</button>';
var orginele_pagina = document.getElementById('ds_body').innerHTML;
document.getElementById('ds_body').innerHTML =orginele_pagina + link_knop + script_source ;
}
main();
How can I get this working?
I need a technique like this, because when the overlay div is called, there will be new functions needed, so if got to inject them then…
Several things:
innerHTMLif you don’t have to. It busts things and leads to the temptation of trying to regex HTML.Putting it all together, your script would become something like: