can't solve this

Discussion in 'Javascript' started by yukatan, Nov 6, 2003.

  1. yukatan

    yukatan Guest

    I have the following Javascript piece of code in a .js file (it's a function
    that generates a new html page with a button to call a function).

    var str = "hello";
    // some code
    with (document) {
    // some writes
    writeln("<TD><A HREF='javascript:myOwnFunction(str);'><IMG
    SRC='images/picture.jpg' BORDER='0'></A></TD></TR>");
    }
    // some writes

    and running it with InternetExplorer6 gives me the error:
    --------------------------------
    Line: 0
    Error: 'str' is not defined
    --------------------------------
    and Mozilla console says the same.

    Seems like it searches the string in a new blank javascript file, but i need
    it uses the same file, where my code is.

    Any help?
    yukatan, Nov 6, 2003
    #1
    1. Advertising

  2. "yukatan" <> writes:

    > I have the following Javascript piece of code in a .js file (it's a function
    > that generates a new html page with a button to call a function).
    >
    > var str = "hello";


    This variable is declared inside a function. That means that it can only
    be seen inside that function's body. The Javascript that you write will
    not be inside this body, so "str" will not be defined for it.

    Solution: move 'var str="hello";' outside the function. That will
    make it a global variable.

    > // some code
    > with (document) {
    > // some writes
    > writeln("<TD><A HREF='javascript:myOwnFunction(str);'><IMG
    > SRC='images/picture.jpg' BORDER='0'></A></TD></TR>");


    Don't use "javascript:".
    <URL:http://jibbering.com/faq/#FAQ4_24>

    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
    Lasse Reichstein Nielsen, Nov 6, 2003
    #2
    1. Advertising

  3. yukatan

    yukatan Guest

    i understand the problem but i cant achieve what i want.
    all the code i wrote before are inside a for loop like this:

    for (int i=0;i<n;++i){
    with (document) {
    writeln("<TD><A HREF='javascript:myOwnFunction(str);'><IMG
    SRC='images/picture.jpg' BORDER='0'></A></TD></TR>");
    }
    }

    so i want to create a table with n rows, but each one calling myOwnFunction
    with the value of "i" as the parameter, that is, row 1 link will call
    myOwnFunction('1'), row 2 link will call myOwnFunction("2"), etc.

    and that is what i cant get it working.
    plz help

    "Lasse Reichstein Nielsen" <> escribió en el mensaje
    news:p...
    > "yukatan" <> writes:
    >
    > > I have the following Javascript piece of code in a .js file (it's a

    function
    > > that generates a new html page with a button to call a function).
    > >
    > > var str = "hello";

    >
    > This variable is declared inside a function. That means that it can only
    > be seen inside that function's body. The Javascript that you write will
    > not be inside this body, so "str" will not be defined for it.
    >
    > Solution: move 'var str="hello";' outside the function. That will
    > make it a global variable.
    >
    > > // some code
    > > with (document) {
    > > // some writes
    > > writeln("<TD><A HREF='javascript:myOwnFunction(str);'><IMG
    > > SRC='images/picture.jpg' BORDER='0'></A></TD></TR>");

    >
    > Don't use "javascript:".
    > <URL:http://jibbering.com/faq/#FAQ4_24>
    >
    > /L
    > --
    > Lasse Reichstein Nielsen -
    > DHTML Death Colors:

    <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    > 'Faith without judgement merely degrades the spirit divine.'
    yukatan, Nov 6, 2003
    #3
  4. yukatan

    Lee Guest

    yukatan said:
    >
    >i understand the problem but i cant achieve what i want.
    >all the code i wrote before are inside a for loop like this:
    >
    >for (int i=0;i<n;++i){
    > with (document) {
    > writeln("<TD><A HREF='javascript:myOwnFunction(str);'><IMG
    >SRC='images/picture.jpg' BORDER='0'></A></TD></TR>");
    >}
    >}
    >
    >so i want to create a table with n rows, but each one calling myOwnFunction
    >with the value of "i" as the parameter, that is, row 1 link will call
    >myOwnFunction('1'), row 2 link will call myOwnFunction("2"), etc.
    >
    >and that is what i cant get it working.


    When you're posting to a technical newsgroup, particularly when
    posting code, it would seem to make sense to take special care
    to follow standard capitalization conventions to make your post
    easier to read.

    You're writing an entirely new page, which will not contain any
    variables from the current page. You can either redefine str in
    the new page, or write its literal value to the new page.
    Also, don't declare variables as "int" and don't use "javascript:".

    html="";
    for (var i=0;i<n;++i){
    html+="<TD><A HREF='#' onclick=myOwnFunction('"
    +str
    +"');'><IMG"
    +"SRC='images/picture.jpg' BORDER='0'></A></TD></TR>";
    }
    document.write(html);
    Lee, Nov 6, 2003
    #4
    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. Ajith Nair
    Replies:
    0
    Views:
    1,630
    Ajith Nair
    Aug 17, 2005
  2. Miguel Dias Moura

    How can i solve this problem?

    Miguel Dias Moura, Dec 10, 2004, in forum: ASP .Net
    Replies:
    9
    Views:
    1,509
    Miguel Dias Moura
    Dec 10, 2004
  3. =?Utf-8?B?S3Vt?=
    Replies:
    1
    Views:
    785
    Peter Blum
    Jun 8, 2005
  4. Kaidi
    Replies:
    1
    Views:
    796
    William Brogden
    Jan 16, 2004
  5. heinz
    Replies:
    9
    Views:
    871
    Roedy Green
    Jun 19, 2004
Loading...

Share This Page