views:

531

answers:

2

I have a master page that adds the jquery library via a registerclientscriptinclude:

Page.ClientScript.RegisterClientScriptInclude(this.GetType(), 
    "JQuery", 
    Page.ResolveUrl("~/Scripts/jquery-1.2.6.min.js"));

In a page that uses that master page I want to include a script that depends on jquery:

Page.ClientScript.RegisterClientScriptInclude(this.GetType(), 
    "jqplugin1", 
    Page.ResolveUrl("~/Scripts/jquery.plugin1.compressed.js"));

This causes a problem because the page's client scripts are written first and then the master pages client scripts (causing the plugin to complain that "jquery" is undefined).

Is there anyway to control the ordering of client script includes? Alternatively any recommendation on how best to control this (temporarily the master page is just including everything... but that's not going to work long term).

+1  A: 

You could re-include the jQuery library in the page, if it's already there it will simply override it, if not it will be added.

GoodEnough
Ouch... that's some ugly :)
I don't think so. The MasterPage needs jQuery so it includes it. Your page also needs it so it includes it. What if your page used a different master page which didn't include jQuery? It wouldn't work.
GoodEnough
Page_PreRender() is the spot. I like that better because I don't know if the browser will recognize a duplicate inclusion or will the js engine reprocess the jquery initialization (which ought not do any harm... its just an inefficiency I'd rather avoid).
+1  A: 

since master page is like a control embedded in a page, you can add these scripts towards the end of the page cycle, the control would fire first and then page, so your page would be fine.

Page_PreRender() is the spot. Thanks!