Loading external scripts

Published Tue, Nov 7 2006 16:51

During beta 1, we could easily load external scripts by adding a <script> element to the page, by using the ClientScriptManager and even by using the ScriptReference element in order to add references to external Javascript files. Beta 2 introduces some changes when you load a script file through a ScriptElement entry. The docs mention this briefly by saying the following:

"Component developers and page developers creating file-based script libraries that are registered
with the ScriptManager control should include a JavaScript code snippet that indicates that the
library has been downloaded by the client. Although this is not required in all browsers,
Safari requires this to be able to dynamically load scripts."

 

After talking with my good friend Garbin (thanks for pointing me in the right direction) and after running some tests, I'd say that this means is that now we must call the notifyScriptLoaded method exposed by the global Sys.Application object. To test this, let's build a simple page. We'll start by defining a Javascript file (test.js):

alert("howdy! I'm an external file" );
if( Sys && Sys.Application ){
   Sys.Application.notifyScriptloaded();
}

Now, you must have noted the test for Sys and Sys.Application. Well, i've put it there since i'm lazy and I'd like to use my libraries in several web apps (which means that it has to run when i have a global Sys.Application object - which allways happens in an AJAX page - and when i don't). btw, i could have gone further and added a test for the method but, as I said, I'm lazy :)

Now, adding the file is really simple, like the following page shows:

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
   <head runat="server">
       <title>Untitled Page</title>
     </head>
<body>
  <form id="form1" runat="server">
      <asp:ScriptManager runat="server" ID="manager">
        <Scripts>
               <asp:ScriptReference Path="test.js" />
        </Scripts>
     </asp:ScriptManager>
   </form>
</body>
</html>

Nothing to difficult, but still something that must be registered for future use.

Filed under: ,

Comments

# Atlas notes said on Tuesday, November 07, 2006 6:29 PM

This is an important change from beta1. In the new beta2 release, scripts added through the ScriptManager

# Atteint de Javascriptite aigüe said on Tuesday, November 07, 2006 8:02 PM

Fox nous a TRES brievement annoncer la mis en ligne de la beta 2 de Microsoft Ajax Extension : ASP.NET

# Mitch said on Sunday, November 12, 2006 2:01 AM

Trying your example produces an odd error error in IE 7.  After the alert displays with the message and I hit ok, I drop into test.js in VS 2005 with the breakpoint at Sys.Application.notifyScriptLoaded() - runtime error: object doesn't support this property or method.

I check off Disable script debugging in IE and get the Error on page after I dismiss the alert that says: Sys.ScriptLoadFailedException:  The script "test.js' could not be loaded or does not contain the Sys.Application.notifyScriptLoaded() callback.

Do I have something configured incorrectly?  Or? Thanks in advance.

Mitch

# Bill Robertson said on Friday, November 17, 2006 4:00 AM

I added a manual ScriptReference to an embedded resource but was receiving a client side error: Error:

# ourblogs said on Friday, December 08, 2006 11:18 PM

I added a manual ScriptReference to an embedded resource but was receiving a client side error: Error:

# Stefan's Blog said on Friday, January 12, 2007 1:31 PM

Gute Sites &amp; Blogs Alex on Asp.nethttp://blogs.dotnetgerman.com/alexonasp.net/Nikhilik.netExtrem...

# ... said on Wednesday, February 28, 2007 2:31 AM

Very nice site! Good work.

# ... said on Saturday, March 31, 2007 2:03 AM

Du musst ein Fachmann sein - wirklich guter Aufstellungsort, den du hast!

# ... said on Monday, April 23, 2007 4:17 PM

http://www.9cicerone.info/piccole

# ... said on Tuesday, April 24, 2007 12:16 PM

http://www.jazrson51.org/sondaggio

# gonzolder said on Tuesday, May 08, 2007 8:23 PM

ja krevetko! Vsem sasat'!

# aarst said on Saturday, May 12, 2007 12:28 PM

higuys!What yourblog powered by?

# Papayum said on Friday, June 29, 2007 9:41 PM

Thank you.

# tron said on Thursday, January 10, 2008 1:20 PM

<a href= http://index1.sdsob.com >curtis w lassiter</a>

# tron said on Friday, January 11, 2008 12:33 AM

<a href= http://index1.sdsay.com >breast nodularity at a posterior depth</a>

# goro said on Monday, January 14, 2008 12:59 PM

<a href= http://index1.gofjf.com >frederick douglass biography</a>

# credit repair said on Sunday, September 27, 2009 12:30 AM

This conversation is titilating! I\'ll have to come back here often!

Leave a Comment

(required) 
(required) 
(optional)
(required)