javascript - Make A Bookmarklet Execute A Function That It Downloads -
i create bookmarklet downloads remote javascript file in function defined , executes function parameters hard-coded bookmarklet.
here standard "download-and-run-remote-script" bookmarklet function call in last line:
javascript:( function () { var new_script = document.createelement("script"); new_script.src = "http://mydomain.com/myscript.js"; document.body.appendchild(new_script); do_stuff('hello world!'); } ) ();
here contents of myscript.js:
function do_stuff(input_variable) { alert(input_variable); }
as written, doesn't anything. why not? should differently?
scripts load asynchronously. means script doesn't finish loading before try run function.
solution 1.) myvar = 'hello world!'
in bookmarklet , do_stuff(myvar)
in myscript.js.
solution 2.) use onload event script element create. more robust, more complicated.
Comments
Post a Comment