0

I have app.js file with a function called getdata(), and another js file called common.js with a function called gethost();

Now I want to call gethost() function in the common.js file using getdata() function in app.js. Can someone please help me to do this.

I don't want to do this script type="text/javascript" src="../Scripts/common.js"

script type="text/javascript" src="../Scripts/app.js"

in aspx file.

2
  • you just need to refer the 2 js files in the web page. Then you can call the functions in both. Commented Feb 27, 2014 at 6:34
  • @nipuna weerasinghe did my suggestion work work Commented Feb 28, 2014 at 6:56

6 Answers 6

1

You should be able to do this just fine as long as you include common.js before app.js

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

Comments

1

Just make sure you are loading the common.js file before the app.js file.

You're essentially putting both functions in the global scope which isn't the best of practices though.

Comments

1

This is the way to do that:

var commonFile = document.createElement('script');
commonFile.src = '/thePathToYourScript/common.js';
document.head.appendChild(commonFile);

Comments

0

It's all about loading order. If common.js is loaded before app.js, it should work. If not, please post some example code.

Comments

0
<script src="common.js" type="text/javascript"/>
<script src="app.js" type="text/javascript"/>

and in your app.js use;

function getdata() {
    ......
    gethost();
    ...

}

Just put common.js before app.js

Edit: If you want to do that without <script src="..., you can use following; in app.js;

$.getScript('path_to_common.js',function(){
   gethost();
});

1 Comment

The OP specifically said not to use "<script src="
0

You can also use jQuery.getScript to get the second JS file into the HTML page.

Say you have, first.js & second.js.

A Sample would be (first.js):

$.getScript("second.js").done(function( script, textStatus ) {

    // call the function from the second js file here

  })
});

Or you could include both in the same HTML, ideally.

1 Comment

Hi All,Thank you very much for your support. Much appreciate your quick reply.

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.