1

I have an html page, I need to add some reference to JS files in the head of the html. The following code is working, but the scriptTVKeyValue is always being added before the tag I would like to add instead directly after Any idea what I am doing wrong here?

<head>
        // I want reference added here
        <script src="js/jquery.js"></script>
        <script src="js/json2.js"></script>
        // Reference to file added here
</head>

// APP_MAIN.onLoad()
            var scriptTVKeyValue = document.createElement('script');
            scriptTVKeyValue.type = 'text/javascript';
            scriptTVKeyValue.src = '$MANAGER_WIDGET/Common/API/TVKeyValue.js';
            head.appendChild(scriptTVKeyValue);

3 Answers 3

1

Look at http://www.jspatterns.com/the-ridiculous-case-of-adding-a-script-element/
You should have the answer

Sign up to request clarification or add additional context in comments.

Comments

1

Solution using jQuery:

//Take the refference to the previous node
var $jsFile = $container.find('[src="js/jquery.js"]'); 
var fileName= '$MANAGER_WIDGET/Common/API/TVKeyValue.js';

// Insert the script
$jsFile .insertAfter($('<script>')
  .attr('type', 'text/javascript')
  .attr('src', fileName));

Comments

1

If you are happy/allowed to use a JS library, then you can load the scripts with requireJS It has advanced features to do this: it will enable you to call functions after your dynamically added script is loaded.

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.