unable to capture all the HTML of my page,or I'm unable to save it to disk

Discussion in 'Javascript' started by Jake Barnes, Feb 8, 2006.

  1. Jake Barnes

    Jake Barnes Guest

    My thanks to everyone who helped answer my last two questions. You've
    all been quite a help to me as I try to learn a bit more about
    Javascript this weekend. I've one more question, though I'm not sure
    this is the right place to ask it. If you go to this page:

    http://www.publicdomainsoftware.org/ajaxExperiment.htm

    and click in a box to get the controls, and maybe add some text, then
    click the "save" link, This function gets triggered:


    function savePage() {
    hideDiv("communicationBox");
    hideDiv("controller");
    hideDiv("deleteBox");
    var pageContent = document.documentElement.innerHTML;

    askForInput("Your page has been saved", "");
    hideDiv("inputBox");
    hideDiv("submitButton");
    referenceToNewDiv = addItem("inputDiv", "div");
    referenceToNewDiv.id = "showSaveResults";

    var url = 'savePage.php';
    var pars = 'pageText=' + pageContent;
    var target = 'showSaveResults';
    var myAjax = new Ajax.Updater(target, url, {method: 'post',
    parameters: pars});
    }


    I've not yet incorporated some of the good advice I've been given on
    this newsgroup. However, on an unrelated topic, I notice now that I'm
    not getting all the HTML. Look at this page, it cut off part way:

    http://www.publicdomainsoftware.org/ajaxTest/Tuesday_02_07_2006_9_48_57_PM.htm

    This was working and now it isn't. I haven't changed my PHP code so
    that can't be the problem (its only 12 lines anyway). The PHP code ads
    the beginning and end HTML tags, but otherwise doesn't do much other
    fwrite() the file to disk.

    Perhaps I should ask in a different forum, but can anyone imagine why I
    would not get all the HTML?
     
    Jake Barnes, Feb 8, 2006
    #1
    1. Advertising

  2. Jake Barnes

    Jake Barnes Guest

    Jake Barnes wrote:
    > My thanks to everyone who helped answer my last two questions. You've
    > all been quite a help to me as I try to learn a bit more about
    > Javascript this weekend. I've one more question, though I'm not sure
    > this is the right place to ask it. If you go to this page:
    >
    > http://www.publicdomainsoftware.org/ajaxExperiment.htm
    >
    > and click in a box to get the controls, and maybe add some text, then
    > click the "save" link, This function gets triggered:
    >
    >
    > function savePage() {
    > hideDiv("communicationBox");
    > hideDiv("controller");
    > hideDiv("deleteBox");
    > var pageContent = document.documentElement.innerHTML;
    >
    > askForInput("Your page has been saved", "");
    > hideDiv("inputBox");
    > hideDiv("submitButton");
    > referenceToNewDiv = addItem("inputDiv", "div");
    > referenceToNewDiv.id = "showSaveResults";
    >
    > var url = 'savePage.php';
    > var pars = 'pageText=' + pageContent;
    > var target = 'showSaveResults';
    > var myAjax = new Ajax.Updater(target, url, {method: 'post',
    > parameters: pars});
    > }
    >
    >
    > I've not yet incorporated some of the good advice I've been given on
    > this newsgroup. However, on an unrelated topic, I notice now that I'm
    > not getting all the HTML. Look at this page, it cut off part way:
    >
    > http://www.publicdomainsoftware.org/ajaxTest/Tuesday_02_07_2006_9_48_57_PM.htm
    >
    > This was working and now it isn't. I haven't changed my PHP code so
    > that can't be the problem (its only 12 lines anyway). The PHP code ads
    > the beginning and end HTML tags, but otherwise doesn't do much other
    > fwrite() the file to disk.
    >
    > Perhaps I should ask in a different forum, but can anyone imagine why I
    > would not get all the HTML?


    Okay, I figured out the answer. Prototype handles the contents of
    paramaters like this:

    toQueryParams: function() {
    var pairs = this.match(/^\??(.*)$/)[1].split('&');
    return pairs.inject({}, function(params, pairString) {
    var pair = pairString.split('=');
    params[pair[0]] = pair[1];
    return params;
    });
    },


    so that made me think that the script was choking on the ampersands

    "&"

    Sure enough, when I look at this page, it ends right before the first
    ampersand:

    http://www.publicdomainsoftware.org/ajaxTest/Tuesday_02_07_2006_10_28_06_PM.htm

    So how should I escape the ampersands?
     
    Jake Barnes, Feb 8, 2006
    #2
    1. Advertising

  3. Jake Barnes

    Zif Guest

    Re: unable to capture all the HTML of my page,or I'm unable to saveit to disk

    Jake Barnes wrote:
    > Jake Barnes wrote:
    >
    >>My thanks to everyone who helped answer my last two questions. You've
    >>all been quite a help to me as I try to learn a bit more about
    >>Javascript this weekend. I've one more question, though I'm not sure
    >>this is the right place to ask it. If you go to this page:
    >>
    >>http://www.publicdomainsoftware.org/ajaxExperiment.htm
    >>
    >>and click in a box to get the controls, and maybe add some text, then
    >>click the "save" link, This function gets triggered:
    >>
    >>
    >> function savePage() {
    >> hideDiv("communicationBox");
    >> hideDiv("controller");
    >> hideDiv("deleteBox");
    >> var pageContent = document.documentElement.innerHTML;
    >>
    >> askForInput("Your page has been saved", "");
    >> hideDiv("inputBox");
    >> hideDiv("submitButton");
    >> referenceToNewDiv = addItem("inputDiv", "div");
    >> referenceToNewDiv.id = "showSaveResults";
    >>
    >> var url = 'savePage.php';
    >> var pars = 'pageText=' + pageContent;
    >> var target = 'showSaveResults';
    >> var myAjax = new Ajax.Updater(target, url, {method: 'post',
    >>parameters: pars});
    >> }
    >>
    >>
    >>I've not yet incorporated some of the good advice I've been given on
    >>this newsgroup. However, on an unrelated topic, I notice now that I'm
    >>not getting all the HTML. Look at this page, it cut off part way:
    >>
    >>http://www.publicdomainsoftware.org/ajaxTest/Tuesday_02_07_2006_9_48_57_PM.htm
    >>
    >>This was working and now it isn't. I haven't changed my PHP code so
    >>that can't be the problem (its only 12 lines anyway). The PHP code ads
    >>the beginning and end HTML tags, but otherwise doesn't do much other
    >>fwrite() the file to disk.
    >>
    >>Perhaps I should ask in a different forum, but can anyone imagine why I
    >>would not get all the HTML?

    >
    >
    > Okay, I figured out the answer. Prototype handles the contents of
    > paramaters like this:
    >
    > toQueryParams: function() {
    > var pairs = this.match(/^\??(.*)$/)[1].split('&');
    > return pairs.inject({}, function(params, pairString) {
    > var pair = pairString.split('=');
    > params[pair[0]] = pair[1];
    > return params;
    > });
    > },
    >
    >
    > so that made me think that the script was choking on the ampersands
    >
    > "&"
    >
    > Sure enough, when I look at this page, it ends right before the first
    > ampersand:
    >
    > http://www.publicdomainsoftware.org/ajaxTest/Tuesday_02_07_2006_10_28_06_PM.htm
    >
    > So how should I escape the ampersands?


    Presumably what you are showing is the character string as your server
    sees it, not the innerHTML property of the page. At a guess, the
    ampersand is being interpreted as a query string name/value pair
    delimiter so your server is accepting just the first parameter.

    Use encodeURIComponent, see the ECMA spec section 15.1.3.2 When you
    send it to the document again, use decodeURIComponent.


    --
    Zif
     
    Zif, Feb 8, 2006
    #3
  4. Jake Barnes

    Jake Barnes Guest

    Zif wrote:
    > Jake Barneswrote:>>
    >>>>http://www.publicdomainsoftware.org/ajaxTest/Tuesday_02_07_2006_9_48_57_PM.htm
    > >>
    > >>This was working and now it isn't. I haven't changed my PHP code so
    > >>that can't be the problem (its only 12 lines anyway). The PHP code ads
    > >>the beginning and end HTML tags, but otherwise doesn't do much other
    > >>fwrite() the file to disk.
    > >>
    > >>Perhaps I should ask in a different forum, but can anyone imagine why I
    > >>would not get all the HTML?

    > >
    > >
    > > Okay, I figured out the answer. Prototype handles the contents of
    > > paramaters like this:
    > >
    > > toQueryParams: function() {
    > > var pairs = this.match(/^\??(.*)$/)[1].split('&');
    > > return pairs.inject({}, function(params, pairString) {
    > > var pair = pairString.split('=');
    > > params[pair[0]] = pair[1];
    > > return params;
    > > });
    > > },
    > >
    > >
    > > so that made me think that the script was choking on the ampersands
    > >
    > > "&"
    > >
    > > Sure enough, when I look at this page, it ends right before the first
    > > ampersand:
    > >
    > > http://www.publicdomainsoftware.org/ajaxTest/Tuesday_02_07_2006_10_28_06_PM.htm
    > >
    > > So how should I escape the ampersands?

    >
    > Presumably what you are showing is the character string as your server
    > sees it, not the innerHTML property of the page. At a guess, the
    > ampersand is being interpreted as a query string name/value pair
    > delimiter so your server is accepting just the first parameter.
    >
    > Use encodeURIComponent, see the ECMA spec section 15.1.3.2 When you
    > send it to the document again, use decodeURIComponent.


    Yes, you were right I think, the problem is the ampersand. How do I
    change the ampersand to something else? I tried every variation I could
    think of on this:

    // var newRegX = /&/g;
    pageContent.replace(/&/g, "qwertyuioplkjhgfdsazxcvbnm");

    // var newRegX = /&/g;
    pageContent.replace(/[&]?/g, "qwertyuioplkjhgfdsazxcvbnm");

    // var newRegX = /&/g;
    pageContent.replace(/\&/g, "qwertyuioplkjhgfdsazxcvbnm");

    and a few other variations, none matched? Any suggestions?


    I'm leary of using encodeURIComponent since it is my PHP code that must
    decode so I won't have access to decodeURIComponent when decoding.
     
    Jake Barnes, Feb 8, 2006
    #4
  5. Jake Barnes

    Jake Barnes Guest

    Jake Barnes wrote:
    > Zif wrote:
    > > Jake Barneswrote:>>
    > >>>>http://www.publicdomainsoftware.org/ajaxTest/Tuesday_02_07_2006_9_48_57_PM.htm
    > > >>
    > > >>This was working and now it isn't. I haven't changed my PHP code so
    > > >>that can't be the problem (its only 12 lines anyway). The PHP code ads
    > > >>the beginning and end HTML tags, but otherwise doesn't do much other
    > > >>fwrite() the file to disk.
    > > >>
    > > >>Perhaps I should ask in a different forum, but can anyone imagine why I
    > > >>would not get all the HTML?
    > > >
    > > >
    > > > Okay, I figured out the answer. Prototype handles the contents of
    > > > paramaters like this:
    > > >
    > > > toQueryParams: function() {
    > > > var pairs = this.match(/^\??(.*)$/)[1].split('&');
    > > > return pairs.inject({}, function(params, pairString) {
    > > > var pair = pairString.split('=');
    > > > params[pair[0]] = pair[1];
    > > > return params;
    > > > });
    > > > },
    > > >
    > > >
    > > > so that made me think that the script was choking on the ampersands
    > > >
    > > > "&"
    > > >
    > > > Sure enough, when I look at this page, it ends right before the first
    > > > ampersand:
    > > >
    > > > http://www.publicdomainsoftware.org/ajaxTest/Tuesday_02_07_2006_10_28_06_PM.htm
    > > >
    > > > So how should I escape the ampersands?

    > >
    > > Presumably what you are showing is the character string as your server
    > > sees it, not the innerHTML property of the page. At a guess, the
    > > ampersand is being interpreted as a query string name/value pair
    > > delimiter so your server is accepting just the first parameter.
    > >
    > > Use encodeURIComponent, see the ECMA spec section 15.1.3.2 When you
    > > send it to the document again, use decodeURIComponent.

    >
    > Yes, you were right I think, the problem is the ampersand. How do I
    > change the ampersand to something else? I tried every variation I could
    > think of on this:
    >
    > // var newRegX = /&/g;
    > pageContent.replace(/&/g, "qwertyuioplkjhgfdsazxcvbnm");
    >
    > // var newRegX = /&/g;
    > pageContent.replace(/[&]?/g, "qwertyuioplkjhgfdsazxcvbnm");
    >
    > // var newRegX = /&/g;
    > pageContent.replace(/\&/g, "qwertyuioplkjhgfdsazxcvbnm");
    >
    > and a few other variations, none matched? Any suggestions?
    >
    >
    > I'm leary of using encodeURIComponent since it is my PHP code that must
    > decode so I won't have access to decodeURIComponent when decoding.


    My mistake. How interesting. I did this:

    pageText = encodeURIComponent(pageText);

    and I sent that to my PHP script. Somewhere along the way it got
    converted back to normal. I did not have to decode it at all.
     
    Jake Barnes, Feb 8, 2006
    #5
  6. Jake Barnes

    VK Guest

    Jake Barnes wrote:
    > How interesting. I did this:
    >
    > pageText = encodeURIComponent(pageText);
    >
    > and I sent that to my PHP script. Somewhere along the way it got
    > converted back to normal. I did not have to decode it at all.


    PHP doesn't use CGI. It uses its own proprietary communication layer
    atop of CGI. Still wondering why the f they did it, but a lot of people
    seems to like it :)
    Here is one of benefits. Not related to JavaScript anyway.
     
    VK, Feb 8, 2006
    #6
  7. Jake Barnes

    9.co.uk Guest

    VK wrote:
    > Jake Barnes wrote:
    > > How interesting. I did this:
    > >
    > > pageText = encodeURIComponent(pageText);
    > >
    > > and I sent that to my PHP script. Somewhere along the way it got
    > > converted back to normal. I did not have to decode it at all.

    >
    > PHP doesn't use CGI. It uses its own proprietary communication layer
    > atop of CGI. Still wondering why the f they did it, but a lot of people
    > seems to like it :)
    > Here is one of benefits. Not related to JavaScript anyway.


    What _are_ you talking about?

    The original poster had problems because his '&' characters were being
    treated as the delimeters between parameters being sent back to his
    php page, prematurely truncating the data.

    Because he's now encoding them properly (encodeURIcomponent will
    change & into %26) this doesn't happen any more.

    Did you mean "PHP automatically decodes %xx constructions" when
    you said it "doesn't use CGI"?
     
    9.co.uk, Feb 8, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jas Shultz
    Replies:
    0
    Views:
    951
    Jas Shultz
    Dec 3, 2003
  2. sincethe2003
    Replies:
    2
    Views:
    554
    Craig Deelsnyder
    Jul 14, 2004
  3. grz02
    Replies:
    3
    Views:
    9,090
    grz02
    Feb 18, 2004
  4. Replies:
    12
    Views:
    523
    santosh
    Nov 15, 2006
  5. Jake Barnes
    Replies:
    15
    Views:
    184
    Thomas 'PointedEars' Lahn
    Feb 7, 2006
Loading...

Share This Page