How to set url calling javascript function correctly?

I am using jslint to test my javascript.

This gives me repeatedly the following error:

Problem at line 236 character 18: Script URL.
a.href = "javascript:DoSomething(" + messageID + ");"

Maybe jslint is right. What would be the correct way to install .href?

+3
source share
6 answers

Give it an event handler onclick, for example:

a.onclick = function() { DoSomething(messageID); };

Leave hrefas #and stop scrolling or return falseto stop scrolling, for example:

a.onclick = function() { DoSomething(messageID); return false; };
+4
source

You should use the event onclick:

<a href="#" onclick="DoSomething(messageID);">Link Text</a>
0
source
<a href="#" onclick="javascript:DoSomething(" + messageID + "); return false;">Link</a>

return false;, a.

0

HREF URL-. "href="javascript:...".

, JavaScript, onclick.

0

The error is that you are trying to set the "click" behavior by changing the "href" first. Do not do that. Instead, give the tag <a>a "click" handler and set "href" to "#" if you don't want the link to go nowhere.

0
source

: document.getElementById('myHref').href = "http://stackoverflow.com"

0
source

Source: https://habr.com/ru/post/1772540/


All Articles