Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript function call through href?

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>");
like image 214
javasundaram Avatar asked Jun 17 '26 14:06

javasundaram


2 Answers

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>');
like image 53
asontu Avatar answered Jun 19 '26 04:06

asontu


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.

like image 40
guhur Avatar answered Jun 19 '26 03:06

guhur



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!