3

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?

1
  • 2
    Wow, the answers for this question are completely pointless. #1 no one addressed your question. #2 people are giving you really bad advice. You should never use onclick. JS handlers should be binded at the JS layer, not inline with onclicks. Commented Jul 29, 2013 at 19:02

6 Answers 6

4

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; };
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks again for helping me with the basics :)
0

You should use the onclick event:

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

Comments

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

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

1 Comment

The leading "javascript:" is unnecessary.
0

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.

Comments

0

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.

Comments

0

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

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.