Hi guy,

It seems that for Web questions, here is better than StackOverflow, so let's try this :-)

I got a markup looking like this :

<body>
    <...
    a lot of tags from the web site
    ...>
    <script type="text/javascript" src="http://cdn.jquerytools.org/1.1.1/jquery.tools.min.js"></script>
    <script type="text/javascript" src="my_script.js"></script>
<body>

I want it to end up that way :

<body>
    <div id="body-content">
        <...
        a lot of tags from the web site
        ...>
        <script type="text/javascript" src="http://cdn.jquerytools.org/1.1.1/jquery.tools.min.js"></script>
        <script type="text/javascript" src="my_script.js"></script>
    </div>
    <div id="isolated-component">
</div>
<body>

So I can populate #isolated-component and easily discard them from a jQuery selection.

I tried that :

jQuery("body").children().wrapAll("<div id='body-content'></div>")
                        .after("<div id='isolated-component'></div>");

But ended up with :

<html>
<head>
<body>
    <div id="body-content">
        <div id="body-content">
            <... page content ...>
        </div>
        <div id="isolated-component"/>
        <div id="isolated-component"/>
    </div>
    <script src="my_script.js" type="text/javascript">
    </script>
</body>
<div id="_firebugConsole" style="display: none;" FirebugVersion="1.4.3" methodName="log"/>
</html>

2 answers

danwellman 5600
0
points

I would use:

$("<div>").attr("id", "isolated-component").insertAfter("#body-content");

Rather than wrapAll() in this case as it would only insert the isolated-component once :)

Answered about 8 years ago by danwellman
  • Just tried your answser. But it produced a similar output. This should work. Maybe the trouble is from another part of the code, but I don't insert anything anywhere else. Strange isn't it ? e-satis about 8 years ago
  • yeah it's weird, can we see the page at all? danwellman about 8 years ago
  • I will try to make something up (it's a complicated dev : php autogenerated bookmarklet ...). e-satis about 8 years ago
0
points

Ok, I found why. When you move the body content to the DIV, it moves the script tags including the JS moving the stuff and lead the script to execute a second time. Now I don't know why this doesn't end up in a infinite loop, but it's good to know : don't ever move script tags, you will make them reload.

Answered about 8 years ago by e-satis
  • excellent debugging :) good to know danwellman about 8 years ago