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 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Sep 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Please help! -- Function argument in link

    this works:
    Code:
    function functest()
    {
          ajax_do('record.php'); //records if the function was executed, this records properly
    }
    document.write("<a href='http://www.google.com' onClick='functest();' target='_blank'>google</a>");
    however, I can't seem to pass an argument in a function; This doesn't work:
    Code:
    function functest(var1)
    {
          if (var1 == 'test')
          {
               ajax_do('record.php'); //records if the function was executed, this doesn't record because the function wasn't executed
          }
    }
    
    document.write("<a href='http://www.google.com' onClick='functest('test');' target='_blank'>google</a>");
    So how can I properly have an argument in a function? I know the problem is in the link, not the function itself. I'm thinking that it has to do with the quotation characters, but I could be wrong...

    thanks to whoever figures this out, I'm stumped and going a wee bit insane.
    Last edited by im.pissed; 09-14-2008 at 07:53 AM. Reason: revision

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,801
    Thanks
    160
    Thanked 2,216 Times in 2,203 Posts
    Blog Entries
    1
    You need to escape your quotes around argument to avoid conflicts
    Code:
    document.write("<a href='http://www.google.com' onClick='functest(\'test\');' target='_blank'>google</a>");
    would work.

    BTW, document.write() is not a standard method, you've to use proper DOM methods to do this.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Quote Originally Posted by abduraooft View Post
    BTW, document.write() is not a standard method, you've to use proper DOM methods to do this.
    As far as I know document.write() is a standard javascript method. It is not a dynamic method, which is something else.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #4
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,801
    Thanks
    160
    Thanked 2,216 Times in 2,203 Posts
    Blog Entries
    1
    Quote Originally Posted by Kor View Post
    As far as I know document.write() is a standard javascript method. It is not a dynamic method, which is something else.
    But, it has some limitations, and that's what I'm trying to say. (http://www.wait-till-i.com/2005/06/2...ed-any-longer/)
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #5
    New to the CF scene
    Join Date
    Sep 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by abduraooft View Post
    Code:
    document.write("<a href='http://www.google.com' onClick='functest(\'test\');' target='_blank'>google</a>");
    I tried this and it still didn't work.


    EDIT: after playing around, I found out that this works:

    Code:
    document.write("<a href='http://www.google.com' onClick='functest(\"test\");' target='_blank'>google</a>");
    thanks for the help
    Last edited by im.pissed; 09-14-2008 at 05:52 PM.

  • #6
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    Quote Originally Posted by im.pissed View Post
    I tried this and it still didn't work.
    Code:
    document.write("<a href='http://www.google.com' onClick='functest(\\'test\\');' target='_blank'>google</a>");
    a single \ escapes only for the current string that began with " since you want the output of that string to be escaped gotta use \\
    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow

  • #7
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    You'd better switch the quotes and use lowercase:
    Code:
    document.write('<a href="http://www.google.com" onclick="functest(\'test\')" target="_blank">google</a>');
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

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