We help IT Professionals succeed at work.

Codebehind Call Javascript Funtions Contained in Central js file?

pointeman
pointeman asked
on
Medium Priority
350 Views
Last Modified: 2012-05-06
I would like to be able to call several different javascripts from a central .js file in a central script folder. Looks like a nice tidy way to do.......

protected void Button1_Click(object sender, EventArgs e)
{
      call ~/script/JScript.js function MessageBoxAlert(textstring)
}

Comment
Watch Question

Create a javascript file in your project

Reference the file in the head tag of your page/master page

<head>
   <script type="text/javascript" src="myjavascript.js"></script>
</head>

Call the javascript from anywhere in your code.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
examples:

//javascript file
function myAlert() {
    alert('Test');
}

//HTML file
<asp:Button OnClientClick="return myAlert();" runat="server" />

//Code Behind if you have a ScriptManager control on your page
ScriptManager.RegisterStartup(this, this.GetType(), "myKey", "myAlert();", true);

//Code Behind if you don't have a ScriptManager
ClientScript.RegisterClientScriptBlock(this.GetType(), "myKey", "myAlert();", true);
You may also do it to add attribute to the Button control during the page load as,
     Button1.Attributes.Add("onclick", "MyFunction();");
To just call a javascript function, you never need to go server-side to do that.
You never NEED to go server-side to call javascript but you may WANT to if you have some C#/VB code that you want to run first.

Author

Commented:
Okay, placed your example function in my master.js file, then performed drag-n-drop file onto the actual aspx page to obtain correct script file reference link like so. The <script src> located on the master.master page did not work probably because my page is located in another sub directory. Anyway this works...
Q. What does "mykey" refer to?
[.aspx]
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<script src="../scripts/master.js" type="text/javascript"></script>

 --- code ---

</asp:Content>
 [.cs]
protected void Button1_Click1(object sender, EventArgs e)
{
     ScriptManager.RegisterStartupScript(this, this.GetType(), "myKey", "myAlert();", true);
}
It is just a reference name given to the script block that you are registering.  Just provide some descriptive name.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.