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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T00:10:20+00:00 2026-06-05T00:10:20+00:00

I am in a situation where it seems that I must use document.write in

  • 0

I am in a situation where it seems that I must use document.write in a javascript library. The script must know the width of the area where the script is defined. However, the script does not have any explicit knowledge of any tags in that area. If there were explicit knowledge of a div then it would be as simple as this:

<div id="childAnchor"></div>
<script ref...
 //inside of referenced script
 var divWidth = $("#childAnchor").width();
</script>

So, inside of the referenced script, I am thinking of doing using document.write like this:

<script ref...
 //inside of referenced script
 var childAnchor = "z_87127XNA_2451ap";
 document.write('<div id="' + childAnchor + '"></div>');
 var divWidth = $("#" + childAnchor).width();
</script>

However, I do not really like the document.write implementation. Is there any alternative to using document.write here? The reason that I cannot simply use window is that this is inside of a view which is rendered inside of a master view page. Window would not properly get the nested area width.

The area is pretty much in here:

<body>
 <div>
  <div>
   <div>
     AREA

The AREA has no knowledge of any of the other divs.

  • 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-05T00:10:22+00:00Added an answer on June 5, 2026 at 12:10 am

    This just occurred to me, and I remembered your question: the script code can find the script block it is in, so you can traverse the DOM from there. The current script block will be the last one in the DOM at the moment (the DOM still being parsed when the code runs).

    By locating the current script block, you can find its parent element, and add new elements anywhere:

    <!doctype html>
    <html>
        <head>
            <style>
            .parent .before { color: red; }
            .parent .after { color: blue; }
            </style>
        </head>
        <body>
            <script></script>
            <div class="parent">
                <span>before script block</span>
                <script>
                var s = document.getElementsByTagName('script');
                var here = s[s.length-1];
    
                var red = document.createElement("p");
                red.className = 'before';
                red.innerHTML = "red text";
                here.parentNode.insertBefore(red, here);
    
                var blue = document.createElement("p");
                blue.className = 'after';
                blue.innerHTML = "blue text";
                here.parentNode.appendChild(blue);
                </script>
                <span>after script block</span>
            </div>
            <script></script>
        </body>
    </html>​
    

    http://jsfiddle.net/gFyK9/2/

    Note the “blue text” span will be inserted before the “after script block” span. That’s because the “after” span does not exist in the DOM at the moment appendChild is called.


    However there’s a very simple way to make this fail.

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

Sidebar

Related Questions

I understand from the official documentation on unowned relationships that the app must use
I am trying to write some front end Javascript tests that can test drag
Situation I have been working on a project lately where the UI development seems
I've noticed that I can not use all unicode characters in my python source
I have a situation where I must copy one NSManagedObject from the main context
I am using an application required by this project that must utilize regular expression
Edit: It seems that by trying to provide some solutions to my own problem
I know this must be asked a millions times and can't be easy to
I know this must be one of the most asked things at SO, but
I’m using Coldfusion MX 8. I recently had a situation where variables seem to

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.