Reputation: 33
I trying to append scripts in client side: example :
var script = '<script type="text/javascript">document.write(\'<script type="text/javascript" src=""><\\/script>\');</script> '
$('body').html(script );
getting error : Uncaught TypeError: Cannot call method 'hasAttribute' of null
Upvotes: 0
Views: 111
Reputation: 49
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'a.js';
document.head.appendChild(script)
Upvotes: 0
Reputation: 8161
If you want to Dynamically add Scripts inside HTML element -
var script = document.createElement( "script" );
script.type = "text/javascript";
script.src = "script.js";
$("body").append(script);
Upvotes: 0
Reputation: 10378
var script_tag = document.createElement('script');
script_tag.setAttribute("type", "text/javascript");
script_tag.setAttribute("src",
"your js url");
By JAVASCRIPT
// Try to find the head, otherwise default to the documentElement
(document.getElementsByTagName("script")[0] || document.documentElement).insertBefore(script_tag);
By JQUERY
$("head").append(script_tag);
Upvotes: 0
Reputation: 82231
Try this:
var scriptlog =document.createElement('script');
scriptlog.type ='text/javascript';
scriptlog.src =url;
$('body').append( script );
Upvotes: 1