Vishwanath.M
Vishwanath.M

Reputation: 6317

How to call function in one javascript from other javascript

I have to access cellid in index.html from signal.js java script file , cellid is defined in signal.js file. currently am using require function in index.html to get signal object, but this is not working

**index.html**

<!DOCTYPE html>
<html>
  <head>  
  <script type="text/javascript" charset="utf-8" src="signal.js"></script>
  <script type="text/javascript" charset="utf-8">
    var signal = require ("signal");
    alert("CellID : "+  signal.cellID);
    var CellID = signal.cellID;
  </script>
  </head>
</html>

signal.js

var exec = require('cordova/exec'),
    cordova = require('cordova');

var Signal = function() {
    this.imei = null;
    this.operator = null;
    this.cellID = null;
    this.lac = null;
    this.neighbors = {};
    // Create new event handlers on the window (returns a channel instance)
    this.channels = {
        watchingnetwork: cordova.addWindowEventHandler("watchingnetwork")
    };
    for (var key in this.channels) {
        this.channels[key].onHasSubscribersChange = Signal.onHasSubscribersChange;
    }

};

Signal.onHasSubscribersChange = function() {
    exec(signal.status, signal.error, "Signal", "getSignalInfo", []);
}

/**
 * Callback for signal initiated
 *
 * @param {Object} info            keys: imei, isPlugged
 */
Signal.prototype.status = function(info) {
    cordova.fireWindowEvent("watchingnetwork", info);
    if (info) {
        if (signal.imei !== info.imei || signal.operator !== info.operator) {

            if (info.imei == null && signal.imei != null) {
                return; // special case where callback is called because we stopped listening to the native side.
            }

            // Something changed. Fire watching network event

            signal.imei = info.imei;
            signal.operator = info.operator;
            signal.cellID = info.cellID;
            signal.lac = info.lac;
            signal.neighbors = info.neighbors;
        }
    }
};

/**
 * Error callback for signal initiated
 */
Signal.prototype.error = function(e) {
    console.log("Error initializing advanced network plugin: " + e);
};

var signal = new Signal();

module.exports = signal;

how can i do this.?

Upvotes: 1

Views: 789

Answers (2)

qianjiahao
qianjiahao

Reputation: 399

actually , if you want to use the function Signal :

// signal.js
var Signal = function() {

    // ....
};

// use function Signal 
Signal();

if you want to use the Signal() , you must stay in the scope with it .

so...

<script src="signal.js"></script>
<script>
    // some code ...
</script>

the above code is mean that put the code in signal.js file and some code into one scope . when you include the js file : signal.js , just like :

<script>
var Signal = function() {

    // ....
};

// some code ...
Signal()
<script>

include the code into one scope ~

Upvotes: 0

Spencer Wieczorek
Spencer Wieczorek

Reputation: 21575

Just have signal.js be first,

<script src="signal.js"></script>
<script>
    // Your code here
</script>

Then the second <script> can use anything defined in signal.js.

Upvotes: 1

Related Questions