?
Solved

How to call a Java Script method when a hyperlink is pressed.

Posted on 2009-04-23
10
Medium Priority
?
519 Views
Last Modified: 2012-05-06
I have a hyperlink on an html web page. When someone clicks on the hyperlink, I need to call a Java Script method. Does anyone know how to do this ? Can you show me some sample code?


<html>
<head>
<script type="text/javascript">
function displaymessage()
{
alert("Hello World!");
}
</script>
</head>
 
<body>
<form>
<li><a href='?' onClick="return true;" id='homenav' )">Call Java Script Method</a></li>
</form>

Open in new window

0
Comment
Question by:brgdotnet
10 Comments
 
LVL 10

Expert Comment

by:mstrelan
ID: 24221554

<a href='#' onclick="displaymessage();" id='homenav' )">Call Java Script Method</a>

Open in new window

0
 
LVL 10

Accepted Solution

by:
mstrelan earned 800 total points
ID: 24221556
actually remove that extra )
<a href='#' onclick="displaymessage();" id='homenav' ">Call Java Script Method</a>

Open in new window

0
 
LVL 10

Expert Comment

by:mstrelan
ID: 24221562
onclick will be executed first. and then the value of href.

in this case href is an anchor to no where and in some browsers will jump you to the top of the page if you arent already there. to avoid that make sure you return false in the onclick. see below
<a href='#' onclick="displaymessage(); return false;" id='homenav' ">Call Java Script Method</a>

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 19

Assisted Solution

by:erikTsomik
erikTsomik earned 80 total points
ID: 24221699
or do this

<a href='javascript:displaymessage();' id='homenav' )">Call Java Script Method</a>

0
 
LVL 13

Assisted Solution

by:qwerty021600
qwerty021600 earned 80 total points
ID: 24221711
<html>
<head>
<script type="text/javascript">
function displaymessage()
{
alert("Hello World!");
}
</script>
</head>
 
<body>
<form>
<li><a href='#' onClick="displaymessage();" id='homenav' )">Call Java Script Method</a></li>
</form>
0
 
LVL 10

Expert Comment

by:mstrelan
ID: 24221716
Avoid what erikTsomik said. Basically if Javascript is disabled on the client's computer you want to provide them with an alternative. The href attribute should point to some alternative content, maybe another page. But the onclick will give you the fancy javascript content.
0
 
LVL 10

Expert Comment

by:mstrelan
ID: 24221718
Well done qwerty, you repeated my first comment.
0
 
LVL 2

Author Closing Comment

by:brgdotnet
ID: 31574079
Thank you. mstrelan, special thanks for the detailed answer.
0
 

Expert Comment

by:tkonan
ID: 28480141
id='homenav' ???
0
 

Expert Comment

by:tkonan
ID: 28481146
id='homenav' )" ???
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question