Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Apostrpohe and quote nightmare!

Status
Not open for further replies.

AndyGroom

Programmer
May 23, 2001
972
0
0
GB
I understand that apostrophe's and speechmarks can both be used in Javascript to identify a string, and that you should use a backslash to identify an apostrophe as an apostrophe rather than a string delimiter. However, I've got this line of code below and I can't get my head around how to make the syntax correct so that when the line is written to a new page it will work, can someone help?
Code:
  document.write("<a href='javascript:navigateto('" + loc + "')'>");
(I've not back-slashed anything so that I can see where they should go in whoever answers answer!)

- Andy
___________________________________________________________________
If you think nobody cares you're alive, try missing a couple of mortgage payments
 
Personally, I always use double quotes for HTML attributes:

Code:
document.write('<a href="javascript:navigateto(\'' + loc + '\')">');

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
P.S. While we're on the subject of apostrophes, you do not use an apostrophe to denote a plural as you have:

I understand that apostrophe's

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
It should be apostrophies, right? Damn, and I've got an A-Level in English too...

- Andy
___________________________________________________________________
If you think nobody cares you're alive, try missing a couple of mortgage payments
 
Personally I don't bother with escaping (backslashing) quotes in this type of usage, I find it much harder to maintain in the long term, especially if someone else has to maintain the code!

I use concatenation techniques:

Code:
document.write("<a href=" + '"' + "javascript:navigateto('" + loc + "'" + '"' + "); >somewhere</a>");
 
Hi

It may be easier to maintain it later, but you failed to write it right for the first time. The string resulted from that expression is not valid HTML :
Code:
<a href="javascript:navigateto('locvalue'"); >somewhere</a>
Personally I always write it as Dan suggested.

Feherke.
 
JA3395 said:
I find it much harder to maintain in the long term, especially if someone else has to maintain the code!

I don't think I'd be handing my code to someone who couldn't work out the simple matter of escaping quotes!

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top