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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:44:38+00:00 2026-06-14T16:44:38+00:00

This is probably not possible but worth asking. I am dynamically inserting script tags

  • 0

This is probably not possible but worth asking. I am dynamically inserting script tags into my head section of my page. At the moment I am using document.write which many of you will frown upon, but it does do the job fine.

However, for the reasons outlined here, I would like to find an alternative to document.write.

Therefore, I need a function that will do this:

<!-- The document.write writes the new script here synchronously so it will block other scripts -->

<script type="text/javascript">
    // Code here that uses the code from the dynamically inserted script
</script>

Can anybody suggest anything with jQuery or plain javascript that will insert an element on the page, but also meets these requirements:

  1. Places the element at the point of which the function was called. e.g. the script tag gets placed after the script tag that called it
  2. The script gets loaded synchronously and therefore blocks the other scripts until it is complete

Google do this with their Google.load() method, do they use document.write? Surely not…

  • 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-06-14T16:44:39+00:00Added an answer on June 14, 2026 at 4:44 pm

    You can look at this article – http://www.phpied.com/non-onload-blocking-async-js/
    Similar script is used by facebook for loading their SDK asynchronously.

    It’s cross-browser and can load scripts asynchronously even when the html on the page is not valid (even if the head/body tag are missing).

    Here is an example directly from the facebook’s SDK:

      (function(d, debug){
         var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
         if (d.getElementById(id)) {return;}
         js = d.createElement('script'); js.id = id; js.async = true;
         js.src = "//connect.facebook.net/en_US/all" + (debug ? "/debug" : "") + ".js";
         ref.parentNode.insertBefore(js, ref);
       }(document, /*debug*/ false));
    

    You have to make some modifications because it allows you to load the script just a single time (because of if (d.getElementById(id)) {return;}).

    If we look at the script closer we can see a number of benefits:

    • On your page you just must have a script tag (because of the script for async loading) so that’s why it’s getting the first script element
    • It looks for already added SDK
    • It creates new script element with the specified id
    • It sets script’s src
    • It inserts the script element on a specific place
    • It loads the scripts asynchronously so it prevents page blocking
    • The self-invoking function creates short alias for the document object. This makes the script more compact.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is weird and probably not possible but I'll ask anyway. I'm making this
I'm not sure if this possible but probably is fairly simple. I've made a
I know this is probably not possible but let's say I have a model
I have a feeling that this probably is not possible using strictly CSS, but,
This is probably not possible, but here goes: I want to create a struct
Ok, i know that probably this is not possible, but i'll give it a
This is probably not possible with CSS, but maybe I'm wrong: I have a
This is probably not possible, but I have this class: public class Metadata<DataType> where
Okay, I know that 1) this is probably not possible with CSS alone and
I know this probably really simple but Im not sure what im doing wrong...

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.