Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-01-2010, 09:28 AM   PM User | #1
rps
New Coder

 
Join Date: Sep 2010
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
rps is on a distinguished road
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?
rps is offline   Reply With Quote
Old 12-01-2010, 09:35 AM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,036
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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
Philip M is offline   Reply With Quote
Old 12-01-2010, 10:57 AM   PM User | #3
rps
New Coder

 
Join Date: Sep 2010
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
rps is on a distinguished road
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).
rps is offline   Reply With Quote
Old 12-02-2010, 01:15 AM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,200
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 12-03-2010, 10:45 AM   PM User | #5
rps
New Coder

 
Join Date: Sep 2010
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
rps is on a distinguished road
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.
rps is offline   Reply With Quote
Old 12-03-2010, 08:37 PM   PM User | #6
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,200
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 12-03-2010, 08:39 PM   PM User | #7
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,200
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 12-04-2010, 07:48 PM   PM User | #8
rps
New Coder

 
Join Date: Sep 2010
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
rps is on a distinguished road
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.
Quote:
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.
rps is offline   Reply With Quote
Reply

Bookmarks

Tags
firefox, parameters

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:34 AM.


Advertisement
Log in to turn off these ads.