Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript move div to tag end body </body>

Tags:

javascript

How to move div to before tag end body </body>. I'm using openx, I cannot change div content, because the content owned by others. I just put code javascript from openx in order to get banner I put the code after tag <body> before anyting tag in content. And my problem is, when I put javascript from openx to generate banner I get two div parallel, which the banner in <div id="div1"> and <div id="div2"> there are in below tag <body>, I want to <div id="div1"> after tag start body <body> above anyting tag in content. And <div id="div2"> in before tag end body </body> after anyting tag.

This is I get html from openx, as below:

<!DOCTYPE html>
<html>
    <head>
    </head>
    <body>
        <div id="div1"><img src="blablabla" /></div>
        <div id="div2"><img src="blablabla" /></div>
        Here is content other div id or div class,
        I not define example insert after div id footer
        Because this is content owned by others.
        This is justexample content.
    </body>
</html>

and I want to transform to as below:

<!DOCTYPE html>
<html>
    <head>
    </head>
    <body>
        <div id="div1"><img src="blablabla" /></div>
        Here is content other div id or div class,
        I not define, example insert after div id footer
        Because this is content owned by others.
        This is justexample content.
        <div id="div2"><img src="blablabla" /></div>
    </body>
</html>

Because I want to put banner one above content and banner two to below content. I don't want to use CSS position: fixed.

So, possible to move tag div to before tag end body? If possible, please help me code javascript to do it.

Thanks.

like image 242
Loren Ramly Avatar asked Mar 27 '13 18:03

Loren Ramly


People also ask

Can DIVS be outside the body?

You can't put something outside of <body> tag. You have to create a container div with all of your content and place the background to that div. Then your jquery box could be placed above that.

How do you add a div to your body?

Another way to add a div to the body element is to use the document. createElement method. document. createElement is used to create the div.

Can I put script tag after body tag?

The best practice is to put JavaScript <script> tags just before the closing </body> tag rather than in the <head> section of your HTML. The reason for this is that HTML loads from top to bottom. The head loads first, then the body, and then everything inside the body.

Can we place js code in body tag?

JavaScript in <head> or <body> You can place any number of scripts in an HTML document. Scripts can be placed in the <body> , or in the <head> section of an HTML page, or in both.


1 Answers

Plain simple javascript:

document.body.appendChild(document.getElementById('div2'));

To move the node you just use appendChild method.

And the demo http://jsfiddle.net/6GsbB/

like image 126
dfsq Avatar answered Oct 19 '22 18:10

dfsq