0

Basically what I want to do is, using JQuery, add a script directly before the closing body tag on a page.

AKA

SCRIPT HERE
</body>

Is there a way to do this, and if so, how?

  • 1
    When, from where, and why are you trying to do this? i.e. are you trying to do this before the page loads, onload, or sometime after the page has fully loaded? – asifrc May 15 '13 at 06:55

3 Answers3

5

use can use $('body').append('<script></script')

Update: Looks like this maynot work always

So Try

var script   = document.createElement("script");
script.type  = "text/javascript";
script.text  = "alert('voila!');"               // use this for inline script
script.src   = "path/to/your/javascript.js";    // use this for linked script

$('body').append(script)

Demo: Fiddle

Community
  • 1
  • 1
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
2
$('<script>', { src: "path/to/script.js" }).appendTo("body");
Ketan Modi
  • 1,780
  • 1
  • 16
  • 28
0

firstly add a footer div right befor </body> like this <div id="footer"></div> then :

$('#footer').after('<script type="text/javascript">SCRIPT HERE</script>');