Elad Kaplan
Elad Kaplan

Reputation: 33

Error when trying append script

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

Answers (4)

yao tony
yao tony

Reputation: 49

var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'a.js';
document.head.appendChild(script)

Upvotes: 0

Ishan Jain
Ishan Jain

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);

Refrence

Upvotes: 0

Rituraj ratan
Rituraj ratan

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

Milind Anantwar
Milind Anantwar

Reputation: 82231

Try this:

var scriptlog  =document.createElement('script');
scriptlog.type ='text/javascript';
scriptlog.src  =url;
$('body').append( script );

Upvotes: 1

Related Questions