Adding an input box and button into an existing page with javascript bookmarklet
Is it possible to have a javascript bookmarklet to add in a new inpu开发者_如何学JAVAt box into an existing page? Basically I need to have a prompt box to save a variable but I don't want it to pop up, I want it just in the page so I can then have it filled out and submit so the variable can be used in another piece of javascript. Its kind of confusing, lol, but just wondering if this is possible.
Thanks
Yes, it's possible.
For example:
javascript:void(document.body.appendChild(document.createElement('input')));
would simply append an input-box to the document's body.
The void() is needed because of appendChild() has a return-value, so if you dont use void, your location will be forwarded to that return-value.
You can add attributes like you usually do in Javascript. Save the created element into a variable, and then assign the attributes.
An example, how to do this:
javascript:void((function(){var obj=document.body.appendChild(document.createElement('input'));obj.value='someValue';alert('It works, value is \n'+document.body.lastChild.value);})());
精彩评论