Add Javascript function

S

Steven

Hello,

I have 3 files --
1. mainform.aspx
2. mainform.aspx.cs
3. mouse.js

mainform.aspx contains a image button ("zoominimgbut") which will exceute C#
code, whenever the user clicks on it. mainform.aspx.cs is the code behind
for mainform.aspx. Mouse.js contains javascript code which needs to executed
ONLY when the user clicks on "zoominimgbut". But the problem is the
javascript code is executing all the time (meaning even if the user doesn't
click on the image button) . I added this line to the page load /
zoominimgbut on click events : ZoomInImgBut.Attributes.Add("onClick",
"mouse.js"); But its not working.

Any help would be greatly appreciated

Regards
Steven
 
M

Marina

Javascript can't execute a file. What would that mean? It can execute
functions. If you include a js file that has a function, then it can call
that function since it becomes part of the page.
 
S

Steven

Hello Marina,

I'm trying to execute the code in javascript file, when the user clicks on a
button .
I added ZoomInImgBut.Attributes.Add("onClick", "mouse.js"); in code behind
page on page load event.
So, javascript should be running whenever the user clicks on the Zoom In
button. But the problem is javascript is running on the page load itself.

Any suggestions

Regards
Steven
 
M

Marina

Like i said, an event handler in javascript cannot be the name of the file.
And 'mouse.js' is the name of a file. Hence this cannot work!!! Client side
javascript cannot know about files on the server. So it cannot go grab
mouse.js when it needs it. All it can do is call functions - and these
functions have to be on the current page.

If you are including mouse.js, then whatever code is in it, will just run
when the page loads, since you don't have it in a function.

I think you should brush up on how javascript works, since it doesn't sound
like you understand what role it plays in a page, and how it should be used.
 
C

Cor Ligthert

Steven,

It is completly as Marina tells,

Maybe when you add to your JS file in the beginning
Function Steven()
{
..
..
And at the end
}

Than it will (maybe) work with that button click event
ZoomInImgBut.Attributes.Add["onClick"] = "javascript:Steven();";

Assuming that what you tell now is exact as it is.

This all typed in this message withouth any test so watch typos or whatever

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top