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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:18:18+00:00 2026-05-26T10:18:18+00:00

I just can’t fathom the logic, perhaps I’m over thinking it! I need to

  • 0

I just can’t fathom the logic, perhaps I’m over thinking it!

I need to make sure a script’s id attribute is unique before I append it into the DOM.

Here are some examples to show you what I need:

If in the <head> element I had 3 child script elements, like:

<script id="script-0"></script>
<script id="script-1"></script>
<script id="script-2"></script>

I’d want my new script element to have an id of "script-3"

If I had 3 child script elements, like:

<script id="script-0"></script>
<script id="script-2"></script>
<script id="script-3"></script>

I’d want my new script element to have an id of "script-1" or "script-4"

And in this case:

<script id="script-1"></script>
<script id="script-2"></script>

I’d want my new script element to have an id of "script-0" or "script-3"

Just so long as the id attributes of each script element are unique!

In this scenario, each script element is actually doing a JSONP request, and therefore, as it’s asynchronous, another script element may be added to the DOM in the interim and would need it’s own unique id.

After the JSONP request has received a response it is removed from the DOM, hence the gaps in the id‘s, like script-0, script-1, script-3, script-4 and script-6 for example, as the JSONP requests script-2 and script-5 may have received a response and been removed from the DOM. In this case, I’d like a new script element to have either the id script-2, script-5 or, worst case: script-7.

I hope this is clear enough!

I just can’t work out the logic, any help would be very much appreciated!

Oh, and only vanilla JS; no jQuery etc, thank you!

I promise to post an answer to a question on Stackoverflow for every answer posted in reply to this question! Promise!

  • 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-26T10:18:18+00:00Added an answer on May 26, 2026 at 10:18 am

    Is it really necessary to fill the gaps?

    I would imagine keeping a running counter and just incrementing it would be easier to work with, especially considering you can have up to 9,007,199,254,740,992 unique JSONP requests:

    var requestJSONP = (function () {
        var guid = 0;
    
        return function (url) {
            var id = 'script-' + guid++;
    
            var script = document.createElement('script');
            script.id = id;
            script.src = url;
            script.type = 'text/javascript';
    
            script.onload = function () {
                this.parentNode.removeChild(this);
            };
    
            document.getElementsByTagName('head')[0].appendChild(script);
    
            return id;
        };
    })();
    

    The closure is optional, but it will prevent any other script from altering guid.

    Example:

    var scriptId = requestJSONP('/request/resource?callback=handler');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just can't figure out how do I make sure an argument passed to
I just can't seem to figure out how to make efficient and clean looking
I am basically just wondering how can I make this while loop repeat continuously
ive browsed many articles here but I just can't make this work for the
i have this simple code i just can't get it working. <html> <head> <script
I just can't find a satisfying way to handle the Browser's History. Sure there
I just can't get my variables to carry over to the next page. The
I just can't figure this out. I have a script that gets data from
I just can't seem to get this query figured out. I need to combine
I recall doing this before but I just can't remember how I did it,

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.