0

Normally we can use var a = "<a href='index.php'>index</a>"; but I need to have more then 2 ' in the same line of code as it follows:

var status = "<a href='javascript:window.open('atendimento.php','hhchat','top=70,left=70,width=480,height=450,status=no,location=no,statusbar=no');void(0);'><img src='img/atendimento.png' width='160' border='0' /></a>";

So the problem is that it starts and finishes the line code here: 'javascript:window.open('

And I need it to continue till the end of the line .. How can it be done?

2
  • 2
    I would avoid trying to generate HTML as strings. If this is in-browser code, use the DOM functions to create HTML elements (var a = document.createElement('a'); a.href="javascript:...";). On the server side, use a template system that handles the quoting for you. Commented May 24, 2013 at 13:45
  • There are cheap template systems that work fine client-side too - my personal favorite is doT. Commented May 24, 2013 at 13:51

5 Answers 5

5

Use " and escape it:

var status = "<a href='javascript:window.open(\"atendimento.php\",\"hhchat\", ...

Although this is a perfect example why you should use unobtrusive JavaScript to bind events - http://en.wikipedia.org/wiki/Unobtrusive_JavaScript and you won't have this kind of problem.

Sign up to request clarification or add additional context in comments.

Comments

0

You need to escape your characters:

var status = "<a href=\"javascript:window.open('atendimento.php','hhchat','top= ...

More info at W3Schools: http://www.w3schools.com/js/js_obj_string.asp

1 Comment

Ideology aside, I find that certain parts of the website explain the basics of Javascript very well. For example, this is the MDN page which explains escaping in Javascript: developer.mozilla.org/en-US/docs/JavaScript/Guide/… That's a little verbose for someone just learning Javascript.
0

Use backslash to escape a quote inside a string:

"<a href='javascript:window.open(\"atendimento.php\" ....

Or better yet, consider best practice, which is to avoid having your JS code inline in your HTML.

Instead, you should be considering defining a click event defined for your element in separate JS code. This would remove the whole issue of the nested quotes.

1 Comment

Wouldn't a click event not work in the case of a click to open in a new tab (ctr+click for example)?
0

use escape characters as below after href=

var status = "<a href=\"javascript:window.open('atendimento.php','hhchat','top=70,left=70,width=480,height=450,status=no,location=no,statusbar=no');void(0);\"><img  src='img/atendimento.png' width='160' border='0' /></a>"; 

Comments

0

Interchange " and ' and then escape \"

var status = "<a href='javascript:window.open(\"atendimento.php\"...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.