0

Javascript newbie here. I have a javascript function that works nested in an html file, I import an external library abaaso, then declare the function and then call it:

<!DOCTYPE html>
<html>
<head>
    <title>title</title>
    <script src="abaaso.js"></script>
</head>

<body>

<script>
    var baseurl = "https://example.com";
    var baseapi = baseurl + "/api/v1/";
    var api_username = "user";
    var api_key = "key";
    var credentials = "?api_username=" + api_username + "&api_key=" + api_key;
    var rawdata = {};

    (function ($$) {

        /* login */
    login = function(username, password) {
    var calledUrl = baseapi + "user/login/" + credentials;
    calledUrl.post(
        function (content) {
            /*console.log("success" + JSON.stringify(content, null, 4));*/
        },
        function (e) {
            console.log("it failed! -> " + e);
        },
        {
                "username": username,
                "password": password

        },
        {"Accept" : "application/json"}
    );
    }

})(abaaso);

login('test@example.com', 'passwd');

</script>
</body>
</html>

I want to put it in an external .js file and import it and only use the call to the function login('test@example.com', 'passwd');. I don't want to import abaaso from the html file but from the new .js.

How can I do that? Is there a particular structure to respect? Do I need to create a global function to the js file?

Bastian
  • 5,625
  • 10
  • 44
  • 68

1 Answers1

1

I don't want to import abaaso from the html file but from the new .js.

You can't do that. However, you can import both abaaso and the new .js:

<head>
  <title>title</title>
  <script src="abaaso.js"></script>
  <script src="new.js"></script>
</head>
Étienne Miret
  • 6,448
  • 5
  • 24
  • 36
  • I was looking at [this question](http://stackoverflow.com/questions/950087/how-to-include-a-javascript-file-in-another-javascript-file) just now trying to figure it out. Wouldn't it be possible in my case? – Bastian Oct 16 '13 at 13:20
  • Yes, it would be possible, I hadn’t thought about this solution. But it seems me you’re going into unnecessary pain. – Étienne Miret Oct 16 '13 at 13:59