Forum Moderators: open

Message Too Old, No Replies

Problems with quote-characters

We have " and ', but what else?

         

technossomy

9:05 pm on May 16, 2005 (gmt 0)

10+ Year Member



Dear all

I remember a thread on this in this forum, but as search is inactive I'll ask again, since I am running into a similar Javascript problem. Consider:

<div onclick="foo('myLink');">division</div>

In Javascript this would be written as:

strMyLink = "<a href=\"http://www.mysite.com\">";
str += "<div onclick=\"foo(" + strMyLink + ");\">division</div>";

The problem here is of course that I have to escape the quotes around "http://www.mysite.com" with quotes other than " or ', or else the function foo ends inappropriately.

How does one fix this problem in general?

Thanks in advance

Tech

Bernard Marx

11:32 pm on May 16, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Confused <<

strMyLink = "<a href=\"http://www.mysite.com\">";
str += "<div onclick=\"foo(" + strMyLink + ");\">division</div>";

1) str hasn't been initialised.

2) Is the string for the <a> tag (held in strMyLink) really meant to go inside the foo() call?

-----------

Generally, it's easier if you start the nesting with single quotes.

strMyLink = '<a href="http://www.mysite.com">';

With your complicated one above, it's probably easier to build the string, as you have been doing. This may do it though:

'<div onclick="foo(\\\'<a href=\\\"http://www.mysite.com\\\">\\\');">division</div>'

john_k

12:02 am on May 17, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



As per Bernard's second question: What's up with the <a> tag in the onClick event handler? Since the final script is to be fired by the onclick event, it needs to set the document.location.


document.write('<div onclick="foo(\'myLink\';">division</div>');

It is entirely possible that you may need to break up the "div" start and end tags.


document.write('<di'+'v onclick="foo(\'myLink\';">division</d'+'iv>');

technossomy

8:24 pm on May 19, 2005 (gmt 0)

10+ Year Member



Thanks for your guidance. I see I had oversimplified my problem, but solved it nevertheless.

Tech Nossomy