Click here to Skip to main content
15,891,943 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
I have a Javascript file that I have gotten off the internet that I need for my project.
I created a Javascript item and pasted the Javascript code there and named it Javascript.js.

I've imported it in the design page using

HTML
<script type="text/javascript" src="JavaScript.js"></script>



Now, in my code behind file, I need to call this function

var ciphertext = Salsa20.encrypt(key, message, nonce, counter, options);


How do I achieve this?

What I have tried:

All the tutorials online mention what I need to do if I have written the script in the .aspx file. But since I have a separate .js file, I am not sure how to implement this
Posted
Comments
Sergey Alexandrovich Kryukov 12-Feb-16 1:33am    
There is no such thing. JavaScript works on client side, code behind — on the server side. The way you effect execution of JavaScript from the server side is this: you can generated any content (in your case, with ASP.NET), including JavaScript code. This is, in particular, how ClientScriptManager works... You need to understand what Web does, and the life cycle of HTTP request/response...
—SA

There are couple of ways to call it
1. using scriptManager
2. using Page.RegisterStartupScript method (without scriptManager)

withscript manager
C#
ScriptManager.RegisterStartupScript(this, this.GetType(), "methodName","methodName('" + Name + "','Keyword',+'Region');", true);


without script manager
C#
Page.RegisterStartupScript("methodName", "methodName('" + Name + "','Keyword',+'Region');");


Static JavaScript files do not get fed through ASP.NET normally, I think you need to make the function on page and then call it from code behind
 
Share this answer
 
You can't call javascript from your code-behind because javascript is something understood by and executed by the browser. Don't listen to people who talk about RegisterStartupScript etc, they're simply confusing you with misinformation. Your c# code runs on the server in the .net environment and does not understand javascript.

You'll have to either translate what the js function does into c# code you can use (if it is for encrypting things then chances are there are equivalent .net functions), or do multiple-stage processing where you generate a page that simply has a form that does the work and puts the value in a hidden field then automatically submits the form, and your page can read the value that way.
 
Share this answer
 
if (!Page.IsPostBack)
{

if (! Page.IsClientScriptBlockRegistered("MyScript"))
{
Page.RegisterClientScriptBlock("MyScript", "<SCRIPT Language='JavaScript' src='js/OPD/patients/jsfileName.js'></SCRIPT>");
}

dName.Attributes.Add("onClick", "fnName();");
}
 
Share this answer
 
v2
Try this .
1.use webmethod as attribute in target methods.
2.add scriptmanager setting EnablePageMethods as true
OR
C#
Page.ClientScript.RegisterStartupScript(this.GetType(), "myScript", "YourFuncition();", true)


For more information please follow below links

Link 1[^]

Link 2[^]

Regards,
AARIF SHAIKH
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900