简体   繁体   中英

Loading javascript in Span/Div in jquery

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.

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM