5

Whenever i want to add a javascript library programatically, say jquery for example, it generally involves making sure there is a placeholder at the footer of my page, then calling a codebehind method that will take a link to the src as a parameter and return an htmlgeneric control, which is then added to this placeholder.

Is this still the neatest way to do it, even with .net 4.0 out?

3
  • 1
    I personally add all of my JS to the ScriptManager. It helps lower the number of Http calls that the page has to make. ScriptManager1.CompositeScript.Scripts.Add(New ScriptReference("~/Page/To/Jquery.js"))... But this is only if you're already using a ScriptManager on your page Commented Feb 14, 2010 at 14:36
  • Does the page need to be 'ajax enabled' to use this? Commented Feb 14, 2010 at 14:43
  • yup, that's why I said, "only if you're already using a ScriptManager" Commented Feb 14, 2010 at 14:44

4 Answers 4

7

I think a better way is to use the RegisterStartupScript method:
http://msdn.microsoft.com/en-us/library/z9h4dk8y.aspx

And even better in your case RegisterClientScriptInclude:
http://msdn.microsoft.com/en-us/library/kx145dw2.aspx

EDIT:
Here's a sample of RegisterClientScriptInclude:

if (!Page.ClientScript.IsClientScriptIncludeRegistered("myJsInclude"))
   Page.ClientScript.RegisterClientScriptInclude("myJsInclude", "myJsFile.js");

EDIT2:
Here's a sample of an include with RegisterStartupScript:

string jsBlock = "<script src='myJsFile.js'></script>";

if (!Page.ClientScript.IsStartupScriptRegistered("myJsInclude"))
   Page.ClientScript.RegisterStartupScript(typeof(string), "myJsInclude", jsBlock, false);

You should add things like language="text/javascript" to the script tag, but for readability I didn't add them.

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

4 Comments

Problem is, i want to load the javascript at the bottom of the page...after everything else has loaded.
RegisterStartupScript will put the code at the end of your page. You could use that function with addScriptTags = false. And write your own include.
That is not true. RegisterStartupScript or RegisterClientScript will put your script registration close to the end of the page, but not at the very bottom. For example, it will put it before the registration for extender controls and $create statements for general AJAX controls.
@IlyaVolodin - the documentation for RegisterClientScriptInlude says "This method adds a script block at the top of the rendered page."
3

Sorry... I decided to move my comment to an answer.

I personally add all of my JS to the ScriptManager. It helps lower the number of Http calls that the page has to make.

ScriptManager1.CompositeScript.Scripts.Add(New ScriptReference("~/Page/To/Jquery.js"))

But this is only if you're already using a ScriptManager on your page

Also, if you don't want to add it from CodeBehind, you can do it right in your page.

<ScriptManager>
   <CompositeScript>
      <Scripts>
         <-- your scripts in here -->
      </Scripts>
   </CompositeScript>
</ScriptManager>

So by doing this, you're able to add all of your JS to a single HTTP Request rather than having a bunch of different requests all at once.

Then in the ScriptManager tag, you can add LoadScriptsBeforeUI="false" to have them put to the bottom of the page.

3 Comments

upvote for LoadScriptsBeforeUI="false". Nice find. Thanks for sharing.
@vinayakhegde, if you look at the dates on this question, it was asked nearly 6 years ago. Technology is sure to change.
yep I agree ..!! ... I used this one , working fine in webform too ... blog.johnnyreilly.com/2012/10/…
1

Sorry but that was never the cleanest way to inject script into an asp.net page.

Look at the ClientScript object. There are several methods that will suit your needs without resorting to placeholders.

1 Comment

Its not the cleanest, but isnt it the only way to achieve loading a script at the footer of the page?
1

ScriptManager is a good way to do this, as mentioned above. If you are not using MS Ajax and ScriptManager, then I suggest you write your own control. It should be very simple control at that. Add a public variable List and override RenderContents method to walk through your list of strings and render on the page. Sample code:

public class CustomScriptManager : WebControl
{
    private List<string> scripts = new List<string>();

    public List<string> Scripts
    {
        get { return scripts; }
        set { scripts = value; }
    }

    protected override void RenderContents(HtmlTextWriter writer)
    {
        foreach (string script in scripts)
        {
            writer.Write("<script language=\"JavaScript\" type=\"text/javascript\" src=\"" + script + "\"></script>");
        }
    }
}

P.S. I haven't verified above code, but I thing you get the idea.

1 Comment

After remembering how I usually do this, just a small comment. I usually don't do list of strings, but instead do collection of simple controls that have just one parameter: src. This way, you can add JS files to your code directly from aspx page, and don't have to resort to adding them from code-behind every time. But the method in the answer will also work, you just have to add all JS to the page from code-behind.

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.