Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 155591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T10:10:57+00:00 2026-05-11T10:10:57+00:00

I find this excellent code, posted by aemkei as answers to this questions: How

  • 0

I find this excellent code, posted by aemkei as answers to this questions:

  1. How do you dynamically load a javascript file? (Think C’s #include)
  2. Use javascript to inject script references as needed?

You may write dynamic script tags (using Prototype):

new Element('script', {src: 'myBigCodeLibrary.js', type: 'text/javascript'}); 

The problem here is that we do not know when the external script file is fully loaded.

We often want our dependant code on the very next line and like to write something like:

if (iNeedSomeMore){   Script.load('myBigCodeLibrary.js');  // includes code for myFancyMethod();   myFancyMethod();                     // cool, no need for callbacks! } 

There is a smart way to inject script dependencies without the need of callbacks. You simply have to pull the script via a synchronous AJAX request and eval the script on global level.

If you use Prototype the Script.load method looks like this:

var Script = {   _loadedScripts: [],   include: function(script){     // include script only once     if (this._loadedScripts.include(script)){       return false;     }     // request file synchronous     var code = new Ajax.Request(script, {       asynchronous: false, method: 'GET',       evalJS: false, evalJSON: false     }).transport.responseText;     // eval code on global level     if (Prototype.Browser.IE) {       window.execScript(code);     } else if (Prototype.Browser.WebKit){       $$('head').first().insert(Object.extend(         new Element('script', {type: 'text/javascript'}), {text: code}       ));     } else {       window.eval(code);     }     // remember included script     this._loadedScripts.push(script);   } }; 

I found that, the code does not work on IE if the all of them is executed in ‘file://’ protocol, however, it is not the problem since its use case involved real web application.

I tried it once to include http://www.google-analytics.com/urchin.js by google, but from one of web page, but it looks like it cannot request javascript file from different domain.

How we could dynamically add javascript, just like what above scripts does, but from another domain?

  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T10:10:58+00:00Added an answer on May 11, 2026 at 10:10 am

    The security model in modern browsers prevents JavaScript from making cross-domain requests. That has holes (see every website exploit since the beginning of the internet), but using them is more than a little shady and it’s only a matter of time before they’re patched.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

not sure why I find this so difficult. Here is the html code: <tr>
I followed this excellent guide on how to use Facebook login. I modified it
I came across this excellent piece of code over at http://projecteuler.net/ but I'm having
I worked this holiday to find out my code wont work on HTTPS. My
Note that this is not about auto-completion or the excellent code formatter, Alt+Shift+F which
I find this answer and this wiki page to be excellent introductions to memoization
From perldoc perlobj (quoted in this excellent answer ): my $fred = Critter->find(Fred); $fred->display(Height,
I'm converting a classified ad/flat file perl script to use mysql/dbi; I have this
I find this feature of automatic selection to item index 0 in the Listbox
I find this article to be useful for non-ajax request How to handle session

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.