views:

214

answers:

2

Some background. The project is a VS 2005 Web Application using ASP.NET AJAX Extensions 1.0.

Question, How can I reference the JavaScript files that are used by the Scriptmanager control, without using the ScriptManager control or how can I load the ScriptManager JS files earlier (I don't think this is possible as the ScriptManager has to be in the server-side form tag)?

I have a ScriptManager control in a master page, but a lot of times, there are pages that are just good ol' non-async postbacks and I create client-side objects before the script manager has loaded. Now I know it's good practice to load scripts near the end of the markup and CSS at the start, but in my case, some of the scripts I create need to be before, typically in the head tag because for example I'll have a repeater that instantiates a client-side object.

So this is what I'd like:

// My scripts

Right now this is what I do to get around the issue:

(function() {
   var myInterval;

   myInterval = setInterval(function() {
      if (!Type) {
         return;
      }

      clearInterval(myInterval);

      Type.registerNamespace("Awesome");

      Awesome.Widget = function() {
         var _this = this;

         this.myProperty = 'This is awesome';
      }
   }, 40);
})();

or I do this:

var Awesome = {};

Awesome.Widget = function {
   /// Awesome code.
}

As well it would be nice to not load these scripts twice as the ScriptManager control will render at some point since it's in my MasterPage.

Maybe the problem is I'm going about this the wrong way. Any help is greatly appreciated.

A: 

I'm not sure I follow your question but hopefully this will help. The standard method to ensure you have access the the ASP.NET AJAX client-side libraries is to add a ScriptManager control to your Master Page and then to use ScriptManagerProxy controls in Content Pages, User Controls and Web Parts when needed.

The settings from the ScriptManagerProxy controls are combined with those of the ScriptManager in the Master Page to ensure the proper JavaScript code is made available to the page.

Rob Windsor
I have no problem accessing the API once the ScriptManager/ScriptManagerProxy loads. The problem I have is I want to use the *.js files ScriptManager loads before the ScriptManager loads or on pages that don't use AJAX so my custom client-side objects can have access to them.
nickyt
A: 

As you are using the MS AJAX controls, you can take advantage of the automatically wired pageLoad function name, and put the code in there.

Your pageLoad function is called when the document is ready, everything's loaded, and you are good to muck around with the DOM to your hearts content (it's a bit like jQuery's document.ready function, but actually fires slightly later).

More details can be found on the AJAX Client Life-Cycle Events page.

function pageLoad(sender, args){
  Type.registerNamespace("Awesome");

  Awesome.Widget = function() {
     var _this = this;

     this.myProperty = 'This is awesome';
  }
}

If you are not fussed about the document being ready, but can content yourself with all scripts being loaded and parse, you could hook into the init event instead.

// Attach a handler to the init event.
Sys.Application.add_init(applicationInitHandler);

function applicationInitHandler() {
  Type.registerNamespace("Awesome");

  Awesome.Widget = function() {
     var _this = this;

     this.myProperty = 'This is awesome';
  }
}
Zhaph - Ben Duguid