javascript - Alternatives to document.write - Stack Overflow

I am in a situation where it seems that I must use document.write in a javascript library. The script m

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.

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.

Share Improve this question edited May 25, 2012 at 23:03 Travis J asked May 25, 2012 at 22:53 Travis JTravis J 82.4k42 gold badges212 silver badges279 bronze badges 4
  • Can you show a sample of your HTML mark up, and explain where the new stuff's being inserted and in response to what event? – David Thomas Commented May 25, 2012 at 23:14
  • 1 If you get the width right after creating the element, before the rest of the page has loaded, you'll probably get 0. You have to wait until the DOM is loaded, then the browser will already have calculated the layout (except for images). – bfavaretto Commented May 25, 2012 at 23:41
  • 1 @DavidThomas - There is an example right there. 3 nested divs and then a script tag and that is all that is available. However, the 3 is really n, and n in this case is 3. It can vary and you cannot know how many there are. This is dynamic, not static. There is no event that will trigger this. When the script is first encountered, at the top of the script, will be a call to find the width. There is no new stuff, just trying to find the width of the parent div where this script is being referenced. This is part of an initialization process for a UI library. – Travis J Commented May 27, 2012 at 19:55
  • 1 @bfavaretto - That is incorrect. In both scenarios the div element is already loaded onto the DOM. Both methods already suggested in the question will yield the script's parent div's width. – Travis J Commented May 27, 2012 at 19:58
Add a ment  | 

4 Answers 4

Reset to default 4

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/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.

There is no alternative to this method. This is the only way that the script can find the width of the element that it is nested in without knowing anything about the DOM that it is loaded into.

Using document.write() when used appropriately is legitimate. Although, appending a new element to the DOM is preferable, it is not always an available option.

if you know which child element it is you can use the param nth child in jquery. otherwise youll need to iterate through them with each()

Create and append the child like this:

var el = document.createElement('div');
el.id='id';
document.body.appendChild(el);

However I'm not really sure what you want to do with this to get the width of whatever, it will probably return 0.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745406263a4626327.html

相关推荐

  • javascript - Alternatives to document.write - Stack Overflow

    I am in a situation where it seems that I must use document.write in a javascript library. The script m

    9小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信