Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First of all, you don't want to append the form to the document's body, because the form's HTML will then be included in subsequent POST requests, and you probably don't want that, so remove this line:</p> <pre><code>document.body.appendChild(d); </code></pre> <p>Secondly, it appears that multiple submissions to the same URL via bookmarklets are blocked by the browser (in Google Chrome's case anyway). The exact same code, run via an <code>onclick</code> handler on a button works every time. The solution is to add an additional, random parameter to each request, like this:</p> <pre><code>d.action = "http://example.com/do.php?nocache=" + Math.random(); </code></pre> <p>I don't know if this a security or a caching issue, but this fix works.</p> <p>Now, on to the new window. This is because you added the <code>target = "_blank"</code> attribute to the form. Of course, if you don't do that, the form submits in the current window / tab, and you probably don't want the user to leave the page. The first think to try would be using an iframe, appending the form to it, appending the iframe to the document, submitting the form, and then removing the iframe. Of course, you would need a mechanism to know when the form has finished submitting, which would probably land you in "cross-domain" restrictions land, but it's worth a try. You could also look into cross-domain AJAX requests, and JSONP.</p> <p><strong>Update</strong></p> <p>I think I've got it! I figured that you don't need the destination page to notify you when the submit is done, you can rely on the iframe's <code>load</code> event. I've tested this in Chrome and it works perfectly, please test other browsers too. See the code comments:</p> <pre><code>(function () { var html = document.documentElement.innerHTML; /** * the iframe's onload event is triggered twice: once when appending it to the document, * and once when the form finishes submitting and the new URL is loaded */ var loaded = 0; var iframe = document.createElement('iframe'); // unique name, to make sure we don't create any conflicts with other elements on the page iframe.name = 'bookmarklet-' + Math.floor((Math.random() * 10000) + 1); iframe.style.display = 'none'; iframe.onload = function () { // remove the iframe from the document on the second firing of the onload event if (++loaded == 1) { return; } // you can also alert('Done!') here :) document.body.removeChild(iframe); }; var form = document.createElement('form'); form.method = "POST"; form.action = "http://requestb.in/sbnc0lsb?nocache=" + Math.random(); form.target = iframe.name; var textarea = document.createElement('textarea'); textarea.name = 'source'; textarea.value = html; form.appendChild(textarea); iframe.appendChild(form); document.body.appendChild(iframe); form.submit(); })(); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload