Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to place html dynamically right after the body tag but within the body tag? jQuery

I need to add some html right after the body tag so like :

<body>
<p>this is the HTML I need to add</p>
</body>

Here is my jQuery code :

jQuery(document).ready(addPageFlipHTML);

function addPageFlipHTML() {

    jQuery('body') // after body tag
            .after(
                    "<div id='pageflip'><a href='http://sabwd.com'><img src='/test/wp-content/plugins/page-flip/images/page_flip.png' alt='' />" //create this html
                            + "<span class='msg_block'>Subscribe via RSS</span></a></div>");

}

Does anyone know how to do this? I am trying to write a universal plugin and need a definite tag like body to add after but inside.

Thanks in Advance,

Mike

like image 729
Michael Sablatura Avatar asked Feb 22 '11 01:02

Michael Sablatura


1 Answers

To position new content inside the opening <body> element, you want the prepend()(docs) method instead:

jQuery('body') 
        .prepend(
                "<div id='pageflip'><a href='http://sabwd.com'><img src='/test/wp-content/plugins/page-flip/images/page_flip.png' alt='' />" //create this html
                        + "<span class='msg_block'>Subscribe via RSS</span></a></div>");

The reason is that jQuery('body') doesn't select the opening <body> tag, but rather the entire <body></body> element.

So the after()(docs) method was attempting to place the new content after </body>, which is invalid. But .prepend() places new content inside the body (and before any other content that may be inside).

like image 68
user113716 Avatar answered Oct 11 '22 10:10

user113716