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

  • Home
  • SEARCH
  • 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 7614015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:17:44+00:00 2026-05-31T02:17:44+00:00

I want to mimick this behavior: <script src=console.log.1.js></script> <script>console.log(2)</script> <script>console.log(3)</script> That logs out: 1

  • 0

I want to mimick this behavior:

<script src="console.log.1.js"></script>
<script>console.log(2)</script>
<script>console.log(3)</script>

That logs out:

1
2
3

Doing this doesn’t work:

<script>
var x = document.createElement("script");
x.src = "console.log.1.js";
x.async = false;
x.defer = false;
document.body.appendChild(x);
console.log("2");
console.log("3");
</script>

It logs out:

2
3
1

The only way I found so far to achieve it:

<script>
document.write("<scrip" + "t src='console.log.1.js'></scrip" + "t>");
</script>
<script>
console.log("2");
console.log("3");
</script>

Is that really the only way to force synchronous loading of external scripts in all browsers? Why doesn’t setting async=false, defer=false work?

UPDATE

FYI, if someone is wondering, the following document.write inception works (in Chrome..):

<script>
  // http://jsbin.com/avatiw logs "during"
  document.write('<scrip' + 't>console.log("before");document.write("<scrip" + "t src=\\"http://jsbin.com/avatiw\\"></scrip" + "t>");</scrip' + 't>');
  document.write('<scrip' + 't>console.log("after");</scrip' + 't>');
</script>

Works and logs out:

"before"
"during"
"after"
  • 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-31T02:17:46+00:00Added an answer on May 31, 2026 at 2:17 am

    Yes, that’s the only way to force the script to load during the page parsing. Or at least, the only way I’d be willing to believe worked well cross-browser.

    If your script were like this, I could see your thinking:

    <script>
    var x = document.createElement("script");
    x.src = "console.log.1.js";
    x.async = false;
    x.defer = false;
    document.body.appendChild(x);
    </script>
    <script><!-- note the new script element -->
    console.log("2");
    console.log("3");
    </script>
    

    …because in theory, when the parser hits the script element, it suspends everything (because there might be document.write statements) and calls into the JavaScript layer. So you might think, well, adding a script element to the end of the body at that point would insert it between the two.

    But adding a script element via appendChild is just fundamentally different, it’s by nature an asynchronous operation (your code continues while the script is being downloaded, which is not the case with script elements in the markup barring the defer or async attributes). I can’t point at any one spec to say why, but the behavior you’re seeing is exactly what I’d expect. The treatment of script elements inline with the markup is a bit special.

    We can see that it’s the download that’s the issue — at least in Chrome — by comparing the result with using a script element with inline content.

    Using an external file (live copy | live source):

    <script>
    console.log("before");
    (function() {
        var s = document.createElement("script");
        s.src = "http://jsbin.com/avatiw"; // Logs the word "during"
        document.body.appendChild(s);
    })();
    </script>
    <script>
    console.log("after");
    </script>
    

    Result:

    before
    after
    during

    Using inline script (live copy | live source — note that I’ve made no attempt to make this cross-browser, it works in Chrome and Firefox as they support the text property on script elements):

    <script>
    console.log("before");
    (function() {
        var s = document.createElement("script");
        s.text = "console.log('during');";
        document.body.appendChild(s);
    })();
    </script>
    <script>
    console.log("after");
    </script>
    

    Output:

    before
    during
    after
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to mimic the trim behavior of Photoshop (crop the area that is
I want to make an app that partially mimics some of the behavior the
Is there a way to do this? I want to mimic the behavior of
My app ships with a .plist that looks like this: I want the user
I have a slideshow built in flash that I want to mimic in jQuery.
Want my FireFox at work to be in sync with my FireFox at my
It's that time of year again that programmers want to shuffle a list such
This partially mimics AJAX (prototype/php) getting partial status updates during script execution , however
Now I've seen some questions like this, but it's not exactly what I want
I've got a need to create a dynamic proxy in C#. I want this

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.