Dynamically loading references

Last post 03-21-2006 1:42 PM by kevindente. 6 replies.

Sort Posts:

  • Dynamically loading references

    03-16-2006, 11:36 PM
    • Member
      344 point Member
    • kevindente
    • Member since 12-05-2003, 11:27 AM
    • Posts 75
    Is there a way to programatically trigger the loading of "references" (client side javascript libraries)? Looking through the current Atlas code, it looks like the reference loading stuff only covers loading static references defined in XML Script.
  • Re: Dynamically loading references

    03-17-2006, 6:06 AM
    • All-Star
      25,662 point All-Star
    • Luis Abreu
    • Member since 02-12-2005, 6:22 AM
    • Madeira [Portugal]
    • Posts 5,368
    • TrustedFriends-MVPs
    hello.

    not really sure if it is this that you want, but...

    here's a sample page that shows how you can load a script through a path or through the ScriptName property:







    Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
    MyBase.OnLoad(e)

    Dim ref As New Microsoft.Web.UI.ScriptReference()
    ref.Path = "~/ScriptLibrary/Atlas/Debug/AtlasUIGlitz.js"
    manager.Scripts.Add(ref)

    Dim ref2 As New Microsoft.Web.UI.ScriptReference()
    ref2.ScriptName = FrameworkScript.AtlasUIDragDrop
    manager.Scripts.Add(ref2)

    End Sub




    Untitled Page










    --
    Regards,
    Luis Abreu
    email: labreu_at_gmail.com
    EN blog:http://msmvps.com/blogs/luisabreu
  • Re: Dynamically loading references

    03-17-2006, 10:33 AM
    • Member
      344 point Member
    • kevindente
    • Member since 12-05-2003, 11:27 AM
    • Posts 75
    Sorry, my original post wasn't clear. I'd like to do this entirely on the client side - no server-side code at all.
  • Re: Dynamically loading references

    03-17-2006, 12:23 PM
    • All-Star
      25,662 point All-Star
    • Luis Abreu
    • Member since 02-12-2005, 6:22 AM
    • Madeira [Portugal]
    • Posts 5,368
    • TrustedFriends-MVPs
    hello.

    can you tell us what you're trying to achieve? if we know, then maybe we can help you...
    --
    Regards,
    Luis Abreu
    email: labreu_at_gmail.com
    EN blog:http://msmvps.com/blogs/luisabreu
  • Re: Dynamically loading references

    03-17-2006, 12:47 PM
    • Member
      344 point Member
    • kevindente
    • Member since 12-05-2003, 11:27 AM
    • Posts 75
    Imagine, for example, an app that dynamically loads different parts of it's functionality (broken up into separate .JS files) on the fly based on what a user is doing. The script on the page would need to tell the browser to load the correct scripts dynamically, without posting back to the server.

    There's a bunch of code the XML script parser that does this sort of dynamic script loading while parsing the <references> section. But I don't see an API for on-demand script loading outside of that. Perhaps I should submit an enhancement request.
  • Re: Dynamically loading references

    03-17-2006, 1:28 PM
    • Participant
      1,277 point Participant
    • Rama Krishna
    • Member since 01-24-2006, 9:33 PM
    • Atlanta, GA
    • Posts 307
    Atlas doesnot provide anything yet. You can use something like this:
        window.ScriptLoader = new function()
        {
            var queuedScripts = new Array();
            var currentScript = null;
            
            function onScriptLoaded()
            {
    			//Load next
    			doScriptLoad();			
            }
            
            function onScriptError()
            {
    			//Invoke the error handler
    			if (this.errorHandler)
    				this.errorHandler.invoke();
    			
    			//Load next
    			doScriptLoad();
            }
    		
    		function onScriptReadyStateChange()
    		{
    			if(event.srcElement.readyState == "complete" ||
    				event.srcElement.readyState == "loaded")
    				onScriptLoaded();
    		}
    		                
            this.loadScript = function(scriptPath)
            {
    			queuedScripts.push(scriptPath);
    			
    			if (currentScript == null)
    				doScriptLoad();
            }
    		
    		//Does the actual script loading
    		function doScriptLoad()
    		{
    			if (currentScript)
    			{
    				currentScript.onload = onScriptLoaded; //For Mozilla/Opera
    				currentScript.onerror = onScriptError; //For Mozilla/Opera
    				currentScript.onreadystatechange = onScriptReadyStateChange; //For IE
    			}
    			
    			if (queuedScripts.length == 0)
    				return;
    
    			currentScript = document.createElement("SCRIPT");
    			
    			currentScript.onload = onScriptLoaded; //For Mozilla/Opera
    			currentScript.onerror = onScriptError; //For Mozilla/Opera
    			currentScript.onreadystatechange = onScriptReadyStateChange; //For IE
    			currentScript.src = queuedScripts.pop();
    			currentScript.type = "text/javascript";
    			document.getElementsByTagName('head')[0].appendChild(currentScript);
    		}
        }
    
    It loads scripts in a fashion similar to Atlas.
  • Re: Dynamically loading references

    03-21-2006, 1:42 PM
    • Member
      344 point Member
    • kevindente
    • Member since 12-05-2003, 11:27 AM
    • Posts 75
    For the record, the March CTP has added a new ScriptLoader class that looks like it does just was I'm looking for. Cool stuff!


Page 1 of 1 (7 items)