I'm not sure that appending the <script> tag as text forces the
browser to parse and run the script as you would like.   All examples
( 
http://cain.supersized.org/archives/2-Dynamic-loading-of-external-JavaScript-.js-files.html
, http://ajaxpatterns.org/On-Demand_Javascript ,
http://blogs.sun.com/insidemyhead/entry/dynamic_javascript_loading )
use 'proper' DOM element creation, for example:

$( function() {
  var s = document.createElement("script");
  s.id = "ga";
  s.src = "http://www.google-analytics.com/urchin.js";;
  s.type = "text/javascript";
  $("#footer").after(s);
  $("#ga").after('<script type="text/javascript">_uacct =
"UA-2158413-2"; urchinTracker();</script>')
});

Note that document.ready is unnecessary in jQuery, all jQuery needs is
$( function() {...}); .  Also, referencing elements is fastest by id,
there is no need for div#id - it is actually slower!, simply $("#id")
will do.   Consider also $("body").append(s) instead, to add the
script as the absolute last element before the closing </body> tag.

Charles Phillips

On Jul 5, 10:06 am, Skullman <[EMAIL PROTECTED]> wrote:
> Hello!
>
> I guess if any of you have tried to load Google Analytics from JQuery.
> What I want to do is as simple as this:
>
> My original footer:
> ************ footer.php ************
> <div id="footer">something</div>
>
> </body>
> </html>
> ************ /footer.php ************
>
> Instead of put the Google Analytics on the footer, I want to include
> the coce before DOM is loaded:
> ************ header.php ************
> [...]
> <script type="text/javascript" src="/media/js/jquery.js"></script>
> <script type="text/javascript">
> $(document).ready(function(){
>         // analytics code (end of page)
>         $('div#footer').after("\n\n<script src='http://www.google-
> analytics.com/urchin.js' type='text/javascript'></script>\n" +
>
> "<script type='text/javascript'>\n" +
>
> "\t_uacct = 'UA-2158413-2'\n" +
>
> "\turchinTracker();\n" +
>                                                                  "</
> script>");
>
> });</script>
>
> ************ header.php ************
>
> That way, when the DOM is loaded, my footer should look like this:
> ************ mywebsite  ************
> [...]
> <div id="footer">something</div>
>
> <script style="display: none;" src="http://www.google-analytics.com/
> urchin.js" type="text/javascript"></script>
> <script type="text/javascript">
>         _uacct = 'UA-2158413-2'
>         urchinTracker();
> </script>
>
> </body>
> </html>
> ************ /mywebsite ************
>
> I have tried, but google doesn't seem to understand that the JS it's
> been loaded, even it's not "on the page".
>
> Thanks for reading!!
>
> Skullman

Reply via email to