开发者

How to correctly set an URL calling a javascript function?

开发者 https://www.devze.com 2023-01-22 18:36 出处:网络
I am using jslint to check my javascript. It\'s giving me repeatedly the following error: Problem at line 236 character 18: Script URL.开发者_运维技巧

I am using jslint to check my javascript.

It's giving me repeatedly the following error:

Problem at line 236 character 18: Script URL.开发者_运维技巧
a.href = "javascript:DoSomething(" + messageID + ");"

Probably, jslint is right. What would be the correct way to set the .href?


Give it an onclick event handler instead, like this:

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

Leave the href as # and either stop propgation or return false to stop the scroll, for example:

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


You should use the onclick event:

<a href="#" onclick="DoSomething(messageID);">Link Text</a>


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

I add the return false; to prevent the normal behavior of the a.


HREF should only be used for actual URLs. It is considered bad form to use "href="javascript:...".

An action calling JavaScript should go into the onclick attribute.


The mistake is that you're trying to set "click" behavior by changing the "href" in the first place. Don't do that. Instead, give the <a> tag a "click" handler, and set the "href" to "#" if you don't want the link to "go' anywhere.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消