28

I have this file include in my html and I want to call it from another javascript. Please suggest me how should I do it?

<script type="text/javascript" src="js.js">/*
  old:123
  new: 456*/
</script>

I want to include it in my js file, not in the html.

5
  • 1
    There is nothing to call...? Commented Aug 15, 2013 at 20:29
  • 1
    You want to do what? Also.. that way of using JavaScript blocks is invalid and won't work/ Commented Aug 15, 2013 at 20:29
  • check this out stackoverflow.com/questions/16839698/… Commented Aug 15, 2013 at 20:31
  • script block is in the html's head.. not in the js file. I want to do the same using another js file. Commented Aug 15, 2013 at 20:32
  • You can use server side languages to generate dynamic js scripts so calling it with a parameter will generate something else. Thats all i can offer Commented Jun 10, 2016 at 15:55

1 Answer 1

71

If you want to include a JS file in a JS you can use jQuery.getScript() for that

$.getScript('another_file.js', function() {
    //script is loaded and executed put your dependent JS here
});

And if you cannot use jQuery

var imported = document.createElement('script');
imported.src = '/path/to/imported/script';
document.head.appendChild(imported);

Source

For more information about the current CSP3 spec from W3 look here.

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

7 Comments

How to pass the parameters? old:123 new: 456
for what you have to pass the parameter??
@Gaurav check out this article feather.elektrum.org/book/src.html
to the js like the linkedIn js api
@Gaurav I would add those LinkedIn variables in the script then if you are setting them
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.