1

I'm trying to load a javascript script link in div/span.(Script contains some image and link) script must come inside span when I run the page.

I have tried with load(),$.getscript but unfortunately I was unable to do so.My script must run as soon as document load without any click or any user action.

HTML

<span class=" loader"></span>

Jquery:

<script type="text/javascript">   

$.ajax({
  type: "GET",
  url: "test.js",
  dataType: "script"
  success: function( strHTML ){
$( ".loader" ).html( strHTML );
});

</script>

I tried this also:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Home</title>
<script src="https://code.jquery.com/jquery-1.11.1.js" type="text/javascript"></script>
<style>

</style>

<script>
$(document).ready(function() 
     {
        var s = document.createElement("script");
        s.type = "text/javascript";
        s.src = "test.js";
        // Use any selector
        $(".loader").append(s);
     });
     </script>
</head>
<body>
 <span class="loader">

  </span>
</body>


</html>

Any help will be helpful.

Mike Phils
  • 3,475
  • 5
  • 24
  • 45
  • Is this connected to your problem: http://stackoverflow.com/questions/950087/how-to-include-a-javascript-file-in-another-javascript-file ?? – Beri Oct 28 '14 at 14:43
  • `$.getScript` is meant for that, it should work. What error you are getting in that? – Rakesh Juyal Oct 28 '14 at 14:50
  • Don't forget to bind `fail` event with `$.getScript` . That will tell you the reason behind failure. – Rakesh Juyal Oct 28 '14 at 14:51

1 Answers1

1

This may not be the right approach, but i tried like below and its working for me...

     $(document).ready(function() 
     {
        var s = document.createElement("script");
        s.type = "text/javascript";
        s.src = "http://scriptlocation/test.js";
        // Use any selector
        $(".loader").append(s);
     });

Can you please refer the below link, it might be helpful Ajax/jQuery - Load webpage content into a div on page load?

Community
  • 1
  • 1
SDK
  • 1,532
  • 1
  • 15
  • 31
  • no i am not gettin error, In developer tool it come as new script inside span(in developer too it is incode is faded format) but it doesn't show up on page. I have added your code also. – Mike Phils Oct 28 '14 at 15:24
  • can you please refer this link http://stackoverflow.com/questions/9963799/ajax-jquery-load-webpage-content-into-a-div-on-page-load – SDK Oct 28 '14 at 15:34