SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a code-behind User Control (myUserControl.ascx and myUserControl.ascx.cs) that are in


and, in the code of myUserControl.ascx I need to use some javascript functions that are in a file myJavascript.js that is in


and I'm trying to import using this:

<script type="text/javascript" language="javascript" src="_scripts/myJavascript.js"></script>

but this way I get a 404 not found exception. How can import this javascript to use in the myUserControl.ascx?

share|improve this question
up vote 5 down vote accepted

I'd recommend you use the ScriptLink control instead. It allows you to inject your JavaScript code into the page header, where they script-tags belong. It also allows you to avoid multiple imports of the same file.

Also, the recommended location for script files are in the /_layouts/ folder ({SharePoint Root}/TEMPLATES/LAYOUTS)

This is how to use the control: In the .ascx:

<SharePoint:ScriptLink name="myjavascript.js" runat="server"/>

Or in the .ascx.cs file (in OnPreRender for instance):

ScriptLink.Register(this.Page, "myjavascript.js", false);
share|improve this answer
Thanks, I changed the scripts to the layouts folder. About, the ScriptLink part, the truth is I've read the link you post, but I don't understand what I have to do. It's a class I have to call from the myUserControl.ascx.cs? from myUserControl.ascx? How it works? Have you any example how use it? Thanks – user674887 Aug 25 '11 at 12:23
Updated the answer with examples of both ways (acsx vs cs) – Wictor Wilen MCA MCM MVP Aug 25 '11 at 12:49
Worked. Thanks! – user674887 Aug 25 '11 at 13:00

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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