Escaping quotes in Javascript
In the below example, should I be escaping the double quotes, such as href="http:
document.write ('');
What about this example? Should I be excaping the double quotes around groups, toolbar, etc?
document.write ('aWindow=window.open(str,\"groups\","toolbar=no,width=" + wth + ",height=" + ht + ",status=no,scrollbars=" + scrl + ",resize=no,menubar=no");');
Suzanne posted this at 01:22 — 30th January 2004.
She has: 5,507 posts
Joined: Feb 2000
no. you should only escape out the single quotes as they will indicate the end of the bit to write. best practice says single quotes for JavaScript, double quotes for HTML. See second example for a way to write it more clearly. false line breaks included for this thread only.
document.write ('<html><head><link href=
"http://www.aftercollege.com/look/aftercollege-site.css"
type="text/css" rel="stylesheet">');
document.write ("aWindow=window.open(str,'groups',
'toolbar=no,width=' + wth + ',height=' + ht + ',status=
no,scrollbars=' + scrl + ',resize=no,menubar=no');");
cat_mother_ posted this at 01:35 — 30th January 2004.
They have: 20 posts
Joined: Jan 2004
Thanks for the reply. I think I need some further clarification, though.
Isn't the normal syntax:
document.write('string');
If that's the case, why is the syntax for the below example:
document.write("html");
document.write ("aWindow=window.open(str,'groups',
'toolbar=no,width=' + wth + ',height=' + ht + ',status=
no,scrollbars=' + scrl + ',resize=no,menubar=no');");
Suzanne posted this at 02:08 — 30th January 2004.
She has: 5,507 posts
Joined: Feb 2000
Because it's easier to read and JavaScript doesn't care.
Basically following the rules of clarity when all else is equal.
cat_mother_ posted this at 04:56 — 30th January 2004.
They have: 20 posts
Joined: Jan 2004
I wasn't aware that javascript would allow
document.write("string"); I thought that it only allowed single quotes in replace of the double quotes in the above example.
Suzanne posted this at 05:38 — 30th January 2004.
She has: 5,507 posts
Joined: Feb 2000
http://developer.irt.org/script/37.htm
As I said, JavaScript doesn't care what quotation marks you use. Just be consistent. In other scripting languages, they mean different things (parse or don't parse), but with JavaScript, they are just delimiters.
More: http://www.iota-six.co.uk/javascript/08_strin.htm
cat_mother_ posted this at 06:40 — 30th January 2004.
They have: 20 posts
Joined: Jan 2004
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.