Wrox Programmer Forums
|
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 5th, 2003, 06:46 PM
Registered User
 
Join Date: Sep 2003
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Quotations

Im storing some HTML inside a JavaScript Variable. I then use that variable as the contents of a div.innerHTML. That part works great. However i have form fields in this html which have onClick events I therfore am presented with 3 levels of Quotations.
string = "<table... "; is my first level to store the whole string
onClick = 'test_func(...' is my second level
I now require a third level, i would normally use in php \" \"
onClick='test_func(\"a\",\"b\");' However when i do this Javascript says that my variable is undefined
so i change \" to \' and it works (up to a point) because it says syntax error but the string is still stored. ive tried every combination i can think of '' '', "" "", and even `` but it doesnt work. Id love to know how i am supposed to do this.
thank you
 
Old October 6th, 2003, 12:32 AM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

In this call the quotes do not have to be escaped:
onClick='test_func("a", "b")'

That is unless you are outputting from PHP, in which case it would depend on whether the statement is enclosed in double or single quotes.

e.g.:
echo "onClick='test_func(\"a\", \"b\")'";
or
echo "onClick=\"test_func('a', 'b')\"";

Speaking purely from the client-side standpoint you do not have to escape single quotes if the statement is enclosed in doubles and vise versa (as is the case in PHP).

You don't need the semi-colon there either.

Does that answer your question?
If not I would need to see more code to know what you are talking about.

: )
Rich



:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
 
Old October 6th, 2003, 02:43 AM
Registered User
 
Join Date: Sep 2003
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

As it stands my first part is written in PHP

$html_string.="<table>";
   $html_string.="<tr>";
      $html_string.="<td width='' align='left'>";
         $html_string.="<input type='text' onFocus='click_name_cat(\'test \');' name='".$f_type."_temp_".$$temp_id_str."_name'>";

I then place the entire string inside a JavaScript Variable

temp_var = "<?echo($html_string);?>";

I then use a function to change the innerHTML of a <div>

document.getElementById('test_div').innerHTML=temp _var;

With the current setup, the innerHTML is changed and i have a new line of form fields, however i am presented with a syntax error

If i use \" instead of \' Then i get the message, temp_var is undefined

Rich, your first example
echo "onClick='test_func(\"a\", \"b\")'";
is what i used first, but that gives me the undefined error
your second example
echo "onClick=\"test_func('a', 'b')\"";
gives me the same thing

thanks

alex
 
Old October 6th, 2003, 03:39 AM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Hi alblinky,

Well your undefined error does not have anything to do with the escaping of quotes in the function call.

I'm not very JavaScript savvy, I know enough to get by! But the basic programming foundation is relatively the same in JavaScript as it is in PHP. Escaping quotes works the same in JavaScript as it does in PHP. If you try to escape quotes in a function call JavaScript will assume you mean the quote literally and then you would likely have some sort of error because you are trying to pass an unquoted string.

Use the alert property to follow your variable's value through the script to try and sort out where things are going awry.

I can with certainty say that it likely has nothing to do with escaping quotes, that would produce a syntax error and not likely a variable with an undefined value unless that undefined value is the result of the syntax error. Such is my educated guess anyway.

if (my_variable == 'undefined')
{

    alert("Error: Aye! My variable is not defined!");

}

Feel free to post more code, maybe someone will be able to see your error.

: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
 
Old October 6th, 2003, 11:03 AM
Registered User
 
Join Date: Sep 2003
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

its all about addslashes()






Similar Threads
Thread Thread Starter Forum Replies Last Post
Writting to an ASCII File from VB6 no quotations kf4rrm VB How-To 2 April 21st, 2006 03:32 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.