SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)

    Dynamically load JS files

    Hello,

    Consider the following code as seen in the Scriptaculous library:

    Code:
      require: function(libraryName) {
        // inserting via DOM fails in Safari 2.0, so brute force approach
        document.write('<script type="text/javascript" src="'+libraryName+'"></script>');
      }
    This will make the browser load more libraries, which is cool.
    However, when using Content-Type application/xhtml+xml, Firefox gives an error in the JS Console:

    Code:
    Error: uncaught exception: [Exception... "Object cannot be created in this context"  code: "9" nsresult: "0x80530009 (NS_ERROR_DOM_NOT_SUPPORTED_ERR)"  location: "http://localhost/script/scriptaculous/scriptaculous.js Line: 26"]
    It seems that XHTML 1.1 documents sent with the correct application/xhtml+xml cause Firefox not to like document.write(). Fair enough

    How can I change the above code to inser the script through DOM? Would that be something like:

    document.createElement('javascript');
    etc...


    Thanks

  2. #2
    SitePoint Enthusiast
    Join Date
    Nov 2005
    Location
    Lucknow, UP, India
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    replace javascript with script
    Who the hell is "General Failure"
    and why is he reading my hard disk???

  3. #3
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    Ah of course.
    I'll give it a go, and post the results!

    EDIT>>

    OK, the following works:

    Code:
      require: function(libraryName) {
    	try {
    		script_tag = document.createElement('script');
    		script_tag.type = 'text/javascript';
    		script_tag.src = libraryName;
    		head = document.getElementsByTagName("head")[0];
    		head.appendChild(script_tag);
    	} catch(e) {
        	// inserting via DOM fails in Safari 2.0, so brute force approach
    	    document.write('<script type="text/javascript" src="'+libraryName+'"></script>');
    	}
      }
    Pity that it's now moaning about bits of HTML being retrieved form the server using Prototype's Ajax functions containing undefined entities (e.g. &nbsp.

    A little more research should help me solve those though

    EDIT 2>>
    Oh yeah, just realised I'd be better off using script_tag.createAttribute('src'); etc

  4. #4
    SitePoint Member
    Join Date
    Oct 2007
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Excellent proposal

    Quote Originally Posted by Immerse View Post
    Ah of course.
    OK, the following works:
    The correct way for me was :
    Code:
     require: function(libraryName) {
    	try {
    		script_tag = document.createElement('script');
    		script_tag.setAttribute('type','text/javascript');
    		script_tag.setAttribute('src',libraryName);
    		head = document.getElementsByTagName("head")[0];
    		head.appendChild(script_tag);
    	} catch(e) {
        	// inserting via DOM fails in Safari 2.0, so brute force approach
    	    document.write('<script type="text/javascript" src="'+libraryName+'"></script>');
    	}
      },
    setAttribute seems to work better AND don't forget the little "," at the end.

    We should propose this fix to scriptaculous, it must happen to more then us. Thanks for the lead ++

    EDIT: I've just proposed the fix to scriptaculous, follow the changes here : Trac ticket 10046
    Last edited by Daddy Cool; Oct 31, 2007 at 15:09. Reason: added follow-up


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •