0

I've found many examples of people calling inline JS functions that are in their HTML pages, but I'm wondering if it is possible to call JS functions that are in their own file.

For example if we have ~/Scripts/mainjs.js where we house most of our Javascript functions, is there any way to call functions in that file from the C# code-behind?

Edit: Also preferably passing parameters to the JS function

1
  • Your C# code runs on your server, while the JavaScript code runs in the client browsers.
    – Pointy
    Jul 23, 2015 at 12:45

2 Answers 2

0

Try Like this

string jsFunc = string.Format("myfunction('{0}');", parameterValue);
ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "myJsFn", jsFunc, true);

Write this in your aspx.cs file Here myfunction is the Function name and parameterValue are the argument.

0

The below way may help you if you are using ASP.Net application:

  1. include the script file in aspx page
  2. create a javascript function on the same aspx page which will call the function from js file
  3. Use the newly created function in your c# code behind file

I know it is lengthy but may resolve your problem.

1
  • Ah I was thinking something like this.. just make an inline function in my HTML page that I will use to pass params/call the function I actually need inside my JS file. I think that is what you mean
    – Justin
    Jul 23, 2015 at 13:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.