Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

js will not enter function 1

Status
Not open for further replies.

gormster

Technical User
Mar 1, 2005
27
AU
I'm relatively new at JavaScript, but as far as I have gone it seems pretty similar to most other OO programming languages. Relatively. Different syntax, same concept. But for some reason, whenever I try to activate a function by clicking on a link, it won't work.

Code:
<script language="JavaScript" type="text/javascript">

//more code here...

function testfunction()
{
	alert("this works");
}

</script></head> <body bgcolor="454549" leftmargin="0">
<script>
var lastsubmitted
</script>
<a class="anchorStyle" onMouseDown="alert('this works')">Slideshow</a><br>
<a class="anchorStyle" onMouseDown="testfunction()">Next</a><br>

I know it's not my HTML, because the straight out "alert('this works')" link (the first one) works perfectly well, but as soon as i put it in a function it refuses to go. What am I doing wrong?

Apologies if this is hopelessly pathetic. As I said, I'm relatively new at JS.
 

Have you tried using "onclick" instead of "onMouseDown"?

Hope this helps,
Dan


[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 
It's not working because and anchor already HAS a onclick action! If you change it to:
Code:
<a class="anchorStyle" href="javascript:alert('this works')">Slideshow</a><br>
<a class="anchorStyle" href="javascript:testfunction()">Next</a>
it will work. Or you could change the anchor tags to some other kind of that that does not normally have an onclick action.


Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
For me the code works both with onMouseDown and onClick, but I'd rather use onClick or href, otherwise the right mouse button will also activate the function.


vlad
 
The code you posted works fine for me. Something tells me we're not getting the whole story.

Tracy, are you saying you can't have an onclick event in an anchor? I'm not so sure about that...

Adam

Whatever I feel like I wanna do, gosh!
 
actually, tracy, the first one works fine, even in an onClick or onMouseDown event inside an anchor tag.

it's only when i call the function.

adam, i've made a bare bones version of my current page with the code i've posted (which is obviously missing a lot of other stuff) and you're right, it works. also, that function was obviously meant to do other stuff.

hmm. wonder what's gone wrong?

by the way, tell me if i'm wrong, but javascript is compiled line by line, right? so if, say, in a function that had an invalid call or something, everything up to that point would be compiled and occur?
 
Perpaps the problem is in the other code.
Remove the additional code and test it. If it works, add in more until you narrow down the problem.

Also, take a look at the javascipt errors (if any): In IE, double click on the icon in the bottom, left corner of the status bar. In Firefox, use the javascript console.
 
thanks Kev! evidently i was wrong about JavaScript...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top