1

I am using C#.NET 2.0. I have already called functions that are stored in external JavaScript file.However, the ,method currently used by me is like this 1) add the external JavaScript file reference e.g.

2) call a local function on click event onclick="someFunction();" This function(someFuction) is present within the design page

3) Then call the function in the external JavaScript file(say ABC()) from within this function.

This works, but there is the overhead of writing the someFunction each time.

Is there a way to call the function ABC() directly form button click ?

Thanks in Advance

1 Answer 1

2

Rohan, I guess you are talking about an ASP.NET web page with a JavaScript block in the header and another separate JavaScript file referenced from the page.

If the external JavaScript file is referenced correctly, you CAN directly call the function.

For example, say you have a JavaScript file named external.js referenced. And lets assume it has a function named ExternalFunction() in it. You can call the ExternalFunction() directly from you HTML or ASP.NET button like you usually call a function in the same file:

Example:

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head runat="server">
    <script type="text/javascript" src="ExternalFile.js"></script>

    <script type="text/javascript">
        // Other local JavaScript code goes here
    </script>
</head>
<body>
    <form runat="server">
    <!-- ASP.NET button control -->
    <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="ExternalFunction();" />
    <!-- HTML button -->
    <input type="button" onclick="ExternalFunction();" />
</body>
</html>

Note that ASP.NET buttons use OnClientClick for JavaScript and HTML buttons use onclick

Sign up to request clarification or add additional context in comments.

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.