简体   繁体   中英

Can't get bookmarklet to POST form with value

The form looks like this:

 <form action='localhost/test.php' method='post' target='test'>
 <input type='text' name='add_to_url' value='' />
 <input type='submit' name='submit' value='Go' />
 </form>

And I can't get anything to even come close.

Ideally the bookmarlet would use the current webpage URL as the add_to_url value, and then submit the form.

Any leads?

To get the url of current page you need to use location.href Javascript property.

To get started with developing bookmarklets, you can go through following links: http://www.bookmarklets.com/tools/categor.html http://betterexplained.com/articles/how-to-make-a-bookmarklet-for-your-web-application/

Also, you can search for bookmarklets Javascript code on Search Bookmarklets

Below is Javascript code to create a form and post it. You use it like get2post('http://site.com?a=1&c=2');

Here is a simple bookmarklet generator, or google for others: http://chris.zarate.org/bookmarkleter

function get2post(u, t) { // u = url, t = target
    var p = u.split('?')[0];
    var q = u.split('?')[1].split('&');
    var d = document;
    var f = d.createElement("form");
    f.setAttribute('action', p);
    f.setAttribute('method', 'POST');
    f.setAttribute('target', t || '_parent');
    f.style.display = 'none';
    for (i = 0; i < q.length; i++) {
        var e = d.createElement("input");
        var param = q[i].split('=');
        e.name = param[0];
        if ( param.length >= 2 ) e.value = decodeURIComponent(param[1]);        
        f.appendChild(e);
    }
    d.body.appendChild(f);
    f.submit();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM