problem with the parameter transmission in function

C

Chris

<< Translation in english of my main post >>
Hi,

I do not success to pass by parameter the contents of "param".
in the function "Main" if I replace Suite('+param+') by Suite('+40+')
dans la fonction Main si je remplace Suite('+param+') par Suite('+40+')
then all is ok.
For info, the parameter "param" will be a string

Thanks for your help
Chris

Corresponding code:

function Main(param) {
alert("test "+param); <<<<<<<<<<< Ici tout se passe bien : le contenu de
param est bien affiché
fchaine=''
....
+'<div ><a href="#" onclick="javascript:return Suite('+param+');"><IMG
src="tg.gif" ></a></div>'
....
document.write(fchaine);
}
-----------------------
function Suite(param) {

alert("test "+formulaire); <<<<<<<<<<< Ici rien ne va plus il est ecrit:
test [objet]
return false;
}
 
R

Richard Cornford

Chris wrote:
For info, the parameter "param" will be a string
+'<div ><a href="#" onclick="javascript:return Suite('+param+');">
<IMG src="tg.gif" ></a></div>'
<snip>

If the value held in - param - is a string then it probably wants to be
quoted within the event handling attribute string. However, when the
string literal used is delimited with single quotes it can only contain
unescaped double quotes. But you are using double quotes to delimit the
HTML attribute values so javascript strings within the HTML attribute
values can only be delimited with shingle quotes. To get around this the
single quotes used to surround - param - in the output need to be
escaped to - \' -

The "javascript:" prefix to the event handling attribute string is
worthless on non-IE browsers and probably redundant on IE. It can be
omitted.


+'<div ><a href="#" onclick="return Suite(\''+param+'\');">'
+'<IMG src="tg.gif" ></a></div>'

Richard.
 
C

Chris

Thanks for your answer Richard.
+'<div ><a href="#" onclick="return Suite(\''+param+'\');">'
+'<IMG src="tg.gif" ></a></div>'

I tried this solution but I obtain in fonction "Suite"

function Suite(param) {

alert("test "+param); <<<<<<<<<<< Display: test+param+ instead of the
contents of the variable param.
test [objet]
return false;
}

If you have an other idea...

Thanks
Chris.
 
R

Richard Cornford

Chris said:
Thanks for your answer Richard.
+'<div ><a href="#" onclick="return Suite(\''+param+'\');">'
+'<IMG src="tg.gif" ></a></div>'

I tried this solution but I obtain in fonction "Suite"

function Suite(param) {

alert("test "+param); <<<<<<<<<<< Display: test+param+ instead
of the contents of the variable param.
test [objet]
return false;
}

If you have an other idea...

I don't need to have other ideas. Based on the information available
that was the correct answer. If it doesn't work then there is something
in the unshown code (or your implementation) that is causing the
problem. No body can debug code they cannot see so you are on your own
until you decide to reveal what you are actually doing.

Richard.
 
R

Richard Cornford

Robert said:
I am not sure what the poster implied by:
return Suite('+param+')

We can assume the OP wants to call Suite. The use of + in this
context is curious. It reminds me of the syntax used in alerts as:
alert("parm = " + parm + ".");

The complete line was:
onclick="javascript:return Suite('+param+');"

The plus symbols are for string concatenation and have a single quote
delimited string literal on each side of them.
This says you want to pass the string "+parma+" to Suite.

The plus symbols themselves should not be making it into the constructed
HTML string. That fact that it appears that they are speaks of
misimplementation or errors elsewhere.
The translation does include the words:
parameter the contents of "param"

This says the OP want to pass the variable parma to Suite.

onclick="javascript:return Suite(param);"

The code string appears to be constructed within a function called -
Main - with - param - as its only formal parameter, so - param - will be
out of scope in any resulting event handlers.

It assume you have declared the global variable
param somewhere else as in:

Me?

The point here is that param has no special meaning in the
context of he <a> tag as this does.

There is also the curious +' at the beginning of the line
that may indicate that the code is coming out of a
document.write or something.
<snip>

I would have said that it was the - document.write(fchaine); - statement
that implied the use of document.write. ;)

Richard.
 
R

Richard Cornford

Robert said:
Richard Cornford wrote:
I think you were right in your first post. The OP wants to generate
the value of param. Perhaps, param contain a single or double quote.
A quote mark would mess up the syntax.

Yes it would, but the symptom of failure in the post responding to my
suggestion was the alerting or "+param+" (with the plus symbols passed
to the - Suite - function in the argument string), which suggests a
different problem (specifically, doing what you think you have seen
instead of what was shown).

document.write('<div ><a href="#" onclick="return Suite(\'' +
escape(param) +
'\');"><IMG src="tg.gif"></a></div>');

I haven't tested this code. Hope the general idea helps.

Using the - escape - method on the string parameter would call for the
use of - unescape - on the argument received by the function in order to
reverse the process. A String.prototype.replace call that inserted an
escape character before single quotes and replaced double quotes with
their hex escape-sequence value (- \x22 -, or used hex-escapes for both,
single is - \x27 -; as the string literals "\\x22" and "\\x27",
respectively) should avoid the potential problem with quote characters
without the need to convert back.

Using scripts to create strings that are used as HTML which creates new
scripts, is always problematic. It is always difficult to see what is
what at each level (and debugging is a nightmare). It gets even worse
when you add dynamically creating the first script with server-side
scripting to the sequence. It is one of those things that is best
avoided (or minimised) at the design stage as it results in an ongoing
maintenance headache.

Richard.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,835
Latest member
lila30

Latest Threads

Top