How can I use pure javascript to append form's action as hidden element?
I'm trying to write a bookmarklet to change the action of all forms on a page to a specified URL, for this example I'm using google.com, and also to add a hidden form element named "action" with the original value.
I've got the first part working:
javascript:(function(){var x,i; x = document.forms; for (i = 0; i < 开发者_Python百科x.length; ++i) x[i].action="http://www.google.com/"; })();
But how do I use pure javascript to append the forms' original action as a new hidden element, like this?
<input type="hidden" name="action" value="http://original-action-url.com" />
if i understand you correctly, This ->
var action = document.forms[0].action;
document.getElementsByName('action')[0].setAttribute("value", action);
Edit (after your comment) -->
y = document.forms;
for (i = 0; i < y.length; ++i){
var x = document.createElement("input");
x.setAttribute("name", "hiddenAction" + i);
x.setAttribute("type","hidden");
x.setAttribute("value",y[i].getAttribute("action"));
y[i].appendChild(x); //you said Appended to the form.
}
Havent tested it, so might require a bit of tweaking. Hope that helps.
精彩评论