1321941
1321941

Reputation: 2180

.ready to load in html

I have the following code. I want to load the content after everything else has loaded. But it seems I can only load html and not javascript:

 $(document).ready(function () {
  $("#twitter_left").html("<p><img align='left' alt='Twitter Bird' src='/sites/all/themes/helixTheme/images/twitter-bird.png' /><strong>&nbsp;&nbsp;Follow Us On Twitter</strong></p><p>&nbsp;</p><p><script charset='utf-8' src='http://widgets.twimg.com/j/2/widget.js'></script><script>new TWTR.Widget({version: 2,type: 'profile',rpp: 7,interval: 30000,width: 'auto',height: 800,theme: {shell: {background: '#dbe5ff',color: '#333333'},tweets: {background: '#ffffff',color: '#999999',links: '#f26422'}},features: {scrollbar: false,loop: true,live: false,behavior: 'default'}}).render().setUser('helixwebsites').start();</script></p>");});

So my next plan was to put the content in a html file and use the .load handler, but I still want it to load after all of the page has.

Upvotes: 0

Views: 148

Answers (3)

Morteza
Morteza

Reputation: 2428

$(document).ready(function () {
  $("body").append("<h1>insert html dynamically</h1>");

  var script = document.createElement('script');
  script.type = 'text/javascript';
  var code = "alert('insert script dynamically');";
  $(script).append(code);
  $("body").append(script);

  var script2 = document.createElement('script');
  script2.type = 'text/javascript';
  script2.src = 'path/your/javascript.js';
  $("body").append(script2);

});

in your code:

$("#twitter_left").html("<p><img align='left' alt='Twitter Bird' src='/sites/all/themes/helixTheme/images/twitter-bird.png' /><strong>&nbsp;&nbsp;Follow Us On Twitter</strong></p><p>&nbsp;</p>");
$("#twitter_left").append(script2);

Load javascript in new window:

$(document).ready(function () {
  var newPage = window.open('', '_blank');
  newPage.document.write("<h1>insert html dynamically</h1>");
  newPage.document.write("<script>alert(1)</" + "script>");
});

Upvotes: 1

gotardo
gotardo

Reputation: 71

Probably, you shall use JQuery getScript function in order to load JavaScript in a dinamic way and then run it.

Upvotes: 1

David Stetler
David Stetler

Reputation: 1461

You can load all the javascript you want. In between the:

$(document).ready(function(){

});

you can write any javascript you want. It can be jQuery or non jQuery javascript code. Example:

$(document).ready(function(){
    alert("Document is now ready");
});

Upvotes: 1

Related Questions