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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:46:09+00:00 2026-06-01T02:46:09+00:00

I created an element in an outer javascript file: var element = document.createElement(‘div’); Now

  • 0

I created an element in an outer javascript file:

var element = document.createElement('div');

Now I need to append the lement to the body and still keep the reference to it (element variable).
But I need to append it right after the script tag – like document.write() would do it.

I can’t use classes and id’s for the script and the element itself – I’m not on my site and there could be more than one of those scripts.

How to solve such task?

CONTEXT: I’m want to build some widgets which could be used on others sites.

  • 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-01T02:46:11+00:00Added an answer on June 1, 2026 at 2:46 am

    How about this:

    if(typeof globalScriptCounter === "undefined")
      var globalScriptCounter = 1;
      else
      globalScriptCounter++;
    
    var scripts = document.getElementsByTagName("script");
    var localCounter = 0;
    for(var i = 0; i < scripts.length; i++) {
      if(scripts[i].src === "yourwidget.js") { // You should know the path since it is your widget
        localCounter++;
    
      if(globalScriptCounter === localCounter) {
        document.body.insertBefore(element, scripts[i]);
        break;
        }
    }
    

    EDIT: I edited my code. Try it out!

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

Sidebar

Related Questions

I want to select recently created element doing something like this: $(body).append(<div id='popup.dialog'></div>); dialogDiv
I need to change the font of element created by the createTextNode() function: var
I'm having problems adding an event to a dynamically created element in Javascript. This
I am trying to lookup elements within a dynamically created element. For some reason
This may be a simple answer, but I'm trying to add a dom-created element
I created a date picker that uses ajax to populate an element with an
Hi I have created a zend_form_element_submit. Now i want to display a confirmation message
My form has a custom element like below, created using custom ajax: <select jwcid=testtest
I've created a form with code and I have a big problem. Every element
I have problems setting the focus on an input element that has been created

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.