Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8739585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:58:00+00:00 2026-06-13T10:58:00+00:00

I’m using a bookmarklet to send the current page source to a server function

  • 0

I’m using a bookmarklet to send the current page source to a server function which then processes it (stores it, no information needs to be returned to the user).

Warning: ugly Javascript ahead, it’s not a language I normally need to use.

Here’s the bookmarklet, pretty-printed for ease of reading:

javascript: (function () {
var h = document.documentElement.innerHTML;

function c(a, b) {
    var c = document.createElement("textarea");
    c.name = a;
    c.value = b;
    d.appendChild(c)
}(document), d = document.createElement("form");
d.method = "POST";
d.action = "http://example.com/do.php";
d.enctype = "multipart/form-data";
d.target = "_blank";
c("h", h);
c("u", "1234");
document.body.appendChild(d);
d.submit();
})();

This basically works, “do.php” receives the head and body sections in form variable h.

However, it only seems to work once for each page. If the bookmarklet is pressed twice for the same page, nothing happens. When the page is reloaded, it works again. Is there something I should be resetting? (running once is enough of course, but it would be nice to give some feedback to the user the second time it is pressed).

Secondly, this pops up a new tab/window. I can work around this by having do.php return some javascript to close the window (note: this is just for testing purposes, not real code):

<?php
$page = $_REQUEST['h'];
file_put_contents('/tmp/work.txt', $page);
echo '<script type="text/javascript">window.close();</script>"';
?>

Ugly. Quick flash of the new tab, then it’s gone. Is there a better approach? A “success” message would be nice, but I can’t see how to incorporate that.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T10:58:02+00:00Added an answer on June 13, 2026 at 10:58 am

    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:

    document.body.appendChild(d);
    

    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 onclick handler on a button works every time. The solution is to add an additional, random parameter to each request, like this:

    d.action = "http://example.com/do.php?nocache=" + Math.random();
    

    I don’t know if this a security or a caching issue, but this fix works.

    Now, on to the new window. This is because you added the target = "_blank" 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.

    Update

    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 load event. I’ve tested this in Chrome and it works perfectly, please test other browsers too. See the code comments:

    (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();
    })();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
I'm making a simple page using Google Maps API 3. My first. One marker
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.