Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    rps
    rps is offline
    New Coder
    Join Date
    Sep 2010
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Passing parameters to another page (Firefox)

    I am passing parameters from one page (actually from a frame within a frameset) to another using Javascript. Typically the code is as follows, taking values from a Form.

    Code:
    parent.titleFrame.location="frm_right_demo_title.html?MyDateEvent.value='"+ytt+
    "'&MyParam_spec.value='"+parent.mainFrame.document.forms.myForm.MyParam_spec.value+"'";
    This has worked fine in all browsers including IE, Chrome and Firefox version 2.0.0.2.

    However, I have now found that it doesn't work with Firefox version 3.6.12.

    This is because when retrieving the parameters in the new loaded page, the character ' (quote) has been converted to %27 (percent twenty seven).

    I can write code to replace %27 with the quote character. However, this is lengthy and time-consuming (as I need to have cyclic code as the javascript replace command seems to work only on the first occurrence within a string).

    Can someone please help me to understand why this is happening and how to overcome it?

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,243
    Thanks
    203
    Thanked 2,555 Times in 2,533 Posts
    Quote Originally Posted by rps View Post

    I can write code to replace %27 with the quote character. However, this is lengthy and time-consuming (as I need to have cyclic code as the javascript replace command seems to work only on the first occurrence within a string).
    To make the replace global (throughout the string) add a /g switch, e.g.

    Code:
    var x = "abababab";
    x = x.replace(/b/gi,"c");
    Sorry - no idea why it doesn't work with Firefox version 3.6.12.


    Made with real ingedients. - Walker's Thai chilli flavour crisps packet

  • #3
    rps
    rps is offline
    New Coder
    Join Date
    Sep 2010
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks Phillip M. That will save time but I still need to understand the reasons please and hope that someone else can help me (and you).

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,128
    Thanks
    80
    Thanked 4,556 Times in 4,520 Posts
    You simply need to use unescape() or decodeURIComponent

    If you show us your code that you are using to read the parameters, we can show you where to put that in.

    Incidentally, FF 3.6 is doing the *RIGHT* thing. Other characters that could get the % treatment would (and should) be = and & and % itself, among others.

    Finally, you are making a big mistake putting the apostrophes and non-standard field names in there in the first place.

    Your code SHOULD be something like:
    Code:
    parent.titleFrame.location = 
        "frm_right_demo_title.html?MyDateEvent="
      + encodeURIComponent(ytt)
      + "&MyParam_spec=" 
      + encodeURIComponent(parent.mainFrame.document.forms.myForm.MyParam_spec.value);
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    rps
    rps is offline
    New Coder
    Join Date
    Sep 2010
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks Old Pedant.

    The code that I am using (typically) for "unpacking" the parameters sent from the originating page (see typical code in my first post) looks like this (with the global replace inserted as advised by Phillip M)...

    Code:
    myparamval = parent.mainFrame.location.search.substr(1)
    
    myparamval = myparamval.replace(/%27/g,"'")
    
    	mySearch = myparamval.split("&")
    	for (x=0;x<=mySearch.length;x++) {
    
    		eval("parent.mainFrame.document.forms.myForm."+mySearch[x])
    		}
    Essentially I send parameters from one page to another using the format after the URL of
    Code:
    ?param1&param2&param3
    In this particular example above I am retrieving values from a Form in the source page and putting them into a Form on the destination page. Therefore I have appended the URL with
    Code:
    ?param1='abc'&param2='def'&param3='ghi'
    If you can clarify how I should be coding this, I would appreciate it, as I am not familiar with unescape or decodeURIComponent. Thanks.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,128
    Thanks
    80
    Thanked 4,556 Times in 4,520 Posts
    Well, assuming you started from
    Code:
    parent.titleFrame.location = 
        "frm_right_demo_title.html?MyDateEvent="
      + encodeURIComponent(ytt)
      + "&MyParam_spec=" 
      + encodeURIComponent(parent.mainFrame.document.forms.myForm.MyParam_spec.value);
    or similar, you would do:
    Code:
    var args = location.substring(1).split("&");
    for ( var a = 0; a < args.length; ++a )
    {
         var pair = args[a].split("=");
         var name = pair[0];
         var value = decodeURIComponent( pair[1] );
         parent.mainFrame.document.myForm[ name ].value = value;
    }
    Try to avoid using eval. Think "eval is evil."
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,128
    Thanks
    80
    Thanked 4,556 Times in 4,520 Posts
    Can you explain to me why you would *GET* the value from
    Code:
         parent.mainFrame.document.myForm.MyParam_spec.value
    and then, apparentely, put it right back in the SAME FORM FIELD????

    At least, I assume that parent.mainFrame refers to the same thing in both the first and second pages.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #8
    rps
    rps is offline
    New Coder
    Join Date
    Sep 2010
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks Old Pedant - I have re-written some of my code to use encodeURIComponent and decodeURIComponent. Okay. Will now work through the whole web site.

    To answer your question.
    put it right back in the SAME FORM FIELD????
    ....

    I display a calendar (in mainFrame) with a whole host of invisible details. When the user clicks on a date it loads a new page (in mainFrame) with a range of options showing a summary of the invisible details. Selecting an option on this page then loads a new page (in mainFrame) with a Form displaying all the invisible details for the user to confirm their intention. The pages are a mixture of Javascript and php (as details are retrieved/stored from/to a MySQL database).

    I'm interested to know why eval is bad news.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •