3

I'm trying to call javascript function (without argument) through href it works fine but same function call (with argument) it through error unexpected end of input

infowindow.setContent("<table><tr><th>Name</th><td><a href='javascript:Institute('"+code+"')'>" + text + "</a></td></tr><tr><th>IP Address</th><td>" + ip + "</td></tr><tr><th>Code</th><td><a href='javascript:Institute();'>" + code + "</a></td></tr></table>");
Pete
58.8k31 gold badges132 silver badges188 bronze badges
asked Jun 12, 2015 at 7:53
5
  • how are you passing arguments? Commented Jun 12, 2015 at 8:01
  • 2
    Mashing together strings in JavaScript to make HTML with JavaScript inside it is a recipe for a headache. Don't do that. Use DOM. Use createElement. Use createTextNode. Use appendChild. Use addEventListener. Your code will be more verbose but infinitely easier to understand, debug and maintain. Commented Jun 12, 2015 at 9:23
  • I'm just being curious : is there an advantage of doing this in href instead of onclick event ? Commented Jun 12, 2015 at 9:28
  • @Quentin if it's a huge chunk of HTML I wouldn't advise using createElement. I agree that addEventListener would be a better option though, OP could get the best of both by giving the clickable elements an id="some_button_id" and then document.getElementById('some_button_id').addEventListener(...) Commented Jun 12, 2015 at 9:34
  • @Niko — No. It makes it more complicated. Should be a button rather than a link too. Commented Jun 12, 2015 at 9:35

3 Answers 3

2

This should work:

infowindow.setContent('<table><tr><th>Name</th><td><a href="javascript:Institute(\''+code+'\')">' + text + '</a></td></tr><tr><th>IP Address</th><td>' + ip + '</td></tr><tr><th>Code</th><td><a href="javascript:Institute();">' + code + '</a></td></tr></table>');

This way the result HTML has double quotes (") for tag attributes and single quotes (') for JavaScript strings, which is the best way to ensure no conflicts arise.

To demonstrate, this uses document.write() and alert() in stead but works if you click Run code snippet

var code = 'The Code';
var text = 'The Text';
var ip = 'The IP';
document.write('<table><tr><th>Name</th><td><a href="javascript:alert(\''+code+'\')">' + text + '</a></td></tr><tr><th>IP Address</th><td>' + ip + '</td></tr><tr><th>Code</th><td><a href="javascript:alert(\'empty string\');">' + code + '</a></td></tr></table>');

answered Jun 12, 2015 at 9:28
Sign up to request clarification or add additional context in comments.

Comments

1

You have a problem with your quote. Use : <a href='javascript:Institute(\'"+code+"\')'>

You could easily find it, if you use the HTML debugger from your web browser.

answered Jun 12, 2015 at 8:03

Comments

0
infowindow.setContent('<table><tr><th>Name</th><td><a href="javascript:Institute(\""+code+"\");' + text + '</a></td></tr><tr><th>IP Address</th><td>' + ip + '</td></tr><tr><th>Code</th><td><a href="javascript:Institute();">' + code + '</a></td></tr></table>');
answered Jun 12, 2015 at 10:10

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.