0

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

0

4 Answers 4

1

Try this:

var scriptlog  =document.createElement('script');
scriptlog.type ='text/javascript';
scriptlog.src  =url;
$('body').append( script );
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

0

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.