problem with the parameter transmission in function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris

    problem with the parameter transmission in function

    << 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="javasc ript:return Suite('+param+' );"><IMG
    src="tg.gif" ></a></div>'
    ....
    document.write( fchaine);
    }
    -----------------------
    function Suite(param) {

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



  • Richard Cornford

    #2
    Re: problem with the parameter transmission in function

    Chris wrote:
    <snip>[color=blue]
    > For info, the parameter "param" will be a string[/color]
    <snip>[color=blue]
    > +'<div ><a href="#" onclick="javasc ript:return Suite('+param+' );">
    ><IMG src="tg.gif" ></a></div>'[/color]
    <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.


    Comment

    • Chris

      #3
      Re: problem with the parameter transmission in function

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

      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.


      Comment

      • Richard Cornford

        #4
        Re: problem with the parameter transmission in function

        Chris wrote:[color=blue]
        > Thanks for your answer Richard.
        >[color=green]
        >> +'<div ><a href="#" onclick="return Suite(\''+param +'\');">'
        >> +'<IMG src="tg.gif" ></a></div>'[/color]
        >
        > 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...[/color]

        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.


        Comment

        • Robert

          #5
          Re: problem with the parameter transmission in function

          In article <cerkjh$m92$1$8 300dec7@news.de mon.co.uk>,
          "Richard Cornford" <Richard@litote s.demon.co.uk> wrote:
          [color=blue][color=green]
          > > +'<div ><a href="#" onclick="javasc ript:return Suite('+param+' );">
          > ><IMG src="tg.gif" ></a></div>'[/color][/color]

          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="javasc ript:return Suite('+param+' );"

          This says you want to pass the string "+parma+" to Suite.

          The translation does include the words:
          parameter the contents of "param"

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

          onclick="javasc ript:return Suite(param);"

          To clarify, onclick specifies a character string of javascript code.
          What means you use normal javascript syntax in the string. The only
          problem is the of quote. The string ends with the second occurrence of
          whatever type of quote you started the string.

          To pass the variable parma to the function Suite, you need to just do:

          onclick="return Suite(param);"

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

          <script>
          var param = 5;
          </script>

          ....

          <body>

          ....

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

          ....

          </body>

          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.

          Which is why Richard mentioned the escaping, I assume. He assume you
          wanted to get the variable when you did the assumed document write.

          I assume you know returning false will stop the link.

          If this doesn't help, we need more information.

          Robert

          Comment

          • Richard Cornford

            #6
            Re: problem with the parameter transmission in function

            Robert wrote:[color=blue]
            > Richard Cornford wrote:[color=green][color=darkred]
            >>> +'<div ><a href="#" onclick="javasc ript:return Suite('+param+
            >>>');"><IMG src="tg.gif" ></a></div>'[/color][/color]
            >
            > 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="javasc ript:return Suite('+param+' );"[/color]

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

            The plus symbols themselves should not be making it into the constructed
            HTML string. That fact that it appears that they are speaks of
            misimplementati on or errors elsewhere.
            [color=blue]
            > The translation does include the words:
            > parameter the contents of "param"
            >
            > This says the OP want to pass the variable parma to Suite.
            >
            > onclick="javasc ript:return Suite(param);"[/color]

            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.

            <snip>[color=blue]
            > It assume you have declared the global variable
            > param somewhere else as in:[/color]

            Me?

            <snip>[color=blue]
            > 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.[/color]
            <snip>

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

            Richard.


            Comment

            • Robert

              #7
              Re: problem with the parameter transmission in function

              In article <cetv9r$1di$1$8 30fa17d@news.de mon.co.uk>,
              "Richard Cornford" <Richard@litote s.demon.co.uk> wrote:
              [color=blue]
              > I would have said that it was the - document.write( fchaine); - statement
              > that implied the use of document.write. ;)
              >
              > Richard.[/color]

              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.

              var param = 5;

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

              generate...

              <div ><a href="#" onclick="return Suite('5');"[color=blue]
              ><IMG src="tg.gif"></a></div>[/color]

              but
              var parma = "Let's begin.";

              gemerate...

              <div ><a href="#" onclick="return Suite('Let's begin.');"[color=blue]
              ><IMG src="tg.gif"></a></div>[/color]

              which would be a problem.

              To avoid problems with quoting, try:

              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.

              Robert

              Comment

              • Richard Cornford

                #8
                Re: problem with the parameter transmission in function

                Robert wrote:[color=blue]
                > Richard Cornford wrote:[/color]
                <snip>[color=blue]
                > 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.[/color]

                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).

                <snip>[color=blue]
                > 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.[/color]

                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.prototyp e.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.


                Comment

                Working...