...

View Full Version : Please help! -- Function argument in link



im.pissed
09-14-2008, 08:19 AM
this works:


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:


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.

abduraooft
09-14-2008, 09:28 AM
You need to escape your quotes around argument to avoid conflicts

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.

Kor
09-14-2008, 09:44 AM
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.

abduraooft
09-14-2008, 09:54 AM
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/21/six-javascript-features-we-do-not-need-any-longer/)

im.pissed
09-14-2008, 06:44 PM
document.write("<a href='http://www.google.com' onClick='functest(\'test\');' target='_blank'>google</a>");

I tried this and it still didn't work. :confused:


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


document.write("<a href='http://www.google.com' onClick='functest(\"test\");' target='_blank'>google</a>");

thanks for the help

shyam
09-14-2008, 06:53 PM
I tried this and it still didn't work. :confused:


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 \\

Kor
09-14-2008, 09:31 PM
You'd better switch the quotes and use lowercase:


document.write('<a href="http://www.google.com" onclick="functest(\'test\')" target="_blank">google</a>');



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum