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 6560127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:25:11+00:00 2026-05-25T13:25:11+00:00

Using JSONP in vanilla JavaScript via creating a <script> tag with a src led

  • 0

Using JSONP in vanilla JavaScript via creating a <script> tag with a src led me to consider using that same method for dependency injection in JavaScript plugins and other JavaScript components that utilize libraries but want to be as independent as possible.

Using jQuery as an example of a popular library that could potentially need to be injected…

// make sure we don't load it if already loaded.
if (typeof jQuery === 'undefined') {
    var dependency = document.createElement('script');
    dependency.src = "https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js";
    document.getElementsByTagName('head')[0].appendChild(dependency);
}

window.onload = function() {
    // jQuery is now available w/o the extra <script> tag added by the end user...
}

Really at this point it’s mainly conceptual (but does work), but I’m wondering if this would be considered a acceptable solution for JavaScript dependency injection? Any thoughts on this?

  • 1 1 Answer
  • 0 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. Editorial Team
    Editorial Team
    2026-05-25T13:25:12+00:00Added an answer on May 25, 2026 at 1:25 pm

    Before implementing this trick, you should consider why it’s needed. It’s acceptable to use the method when your code is vital, and has to be used at unknown environments. When you’re building a web-plugin, for external websites, try to force the developer to include the required libraries. It’s more efficient, and also more reliable.

    Regarding your example: put the code inside an (anonymous) function, to not fill the global scope with unnecessary variables.

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

Sidebar

Related Questions

We are using the Dynamic Script Tag with JsonP mechanism to achieve cross-domain Ajax
I am using the JSONP/dynamic-script-tag technique to perform cross-domain AJAX (There's no XML, but
Is it safe to inject JQuery's script using JsonP? The installation of my web
I'm using JSONP to get data from a server. The more typical method of
I have some data that I'm retrieving using JSONP from a remote server. The
I'm making the following call in jQuery, using jsonp as my data format, that
I plan on using JSONP to call an external web service to get around
Is it possible to catch an error when using JSONP with jQuery? I've tried
I am using Rails 2.3 and I decided to provide support for JSONP. Created
I am using Json.NET to serialize an object graph. For each object that is

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.