Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi to all,
I want to add scripts dynamically...
but scripts not worked for me..
plz help me..

function IncludeScritps()
			{

var scrpt = "http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js,http://mottie.github.com/tablesorter/docs/js/chili/jquery.chili-2.2.js,http://mottie.github.com/tablesorter/docs/js/chili/recipes.js,http://mottie.github.com/tablesorter/docs/js/docs.js,../Include/tablesorter2.0/jquery.tablesorter.js,http://mottie.github.com/tablesorter/js/jquery.tablesorter.widgets.js"

				var JQArgs1 = scrpt.split(",");
		
				for (var i=0; i<jqargs1.length;>				{
					var script=document.createElement('script');
					script.type='text/javascript';
					script.src=JQArgs1[i];
					AddScript(JQArgs1[i],function() {alert($(document).attr('title'));});
					//var n = document.getElementsByTagName('script').length;
					//var sln=parseInt(n)-1;
					//var s = document.getElementsByTagName('script')[document.getElementsByTagName('script').length-2];
					//s.parentNode.insertBefore(script, s);
				}

			}
Posted
Updated 17-Dec-12 23:58pm
v2

1 solution

Sample


In your case you will have to do the same thing over and over for each element on your array of script url. Next, lets load jquery from the document onload event



XML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script type="text/javascript">
        //dynamic script loading
        function InjectScript() {
            var src = "http://code.jquery.com/jquery-1.7.2.min.js";
            var head = document.getElementsByTagName("head")[0]
            var script = document.createElement("script");
            script.type = 'text/javascript';
            script.src = src;
            head.appendChild(script);

            //notify
            alert("jQuery script tag added to head tag");
        }
    </script>
</head>
<body onload="InjectScript();">
</body>
</html>


Hope this helps, cheers.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900