0

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.?

Vishwanath.M
  • 6,235
  • 11
  • 42
  • 56
  • There is not enough information here to answer this question – JoshWillik Apr 18 '15 at 03:04
  • Where is CellID and what is the file you posted? The script that you have posted looks like the Signal file. Can you please clarify? – Norman Breau Apr 18 '15 at 03:09
  • It really depends on how your source code organised. You can just require signal.js and get `cellID` directly from the assigned variable, since signal.js exports Signal instance which contains cellID. That is if your app is in the same environment as your signal.js file i.e. server side nodejs – fudanchii Apr 18 '15 at 03:15
  • @Vishwanath I'm still a bit confused on what environment this is suppose to run in. Is this a website? Or is this a NodeJS app? Browsers don't have a `require` method. – Norman Breau Apr 20 '15 at 05:34

2 Answers2

2

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.

Spencer Wieczorek
  • 21,229
  • 7
  • 44
  • 54
  • I aadded , but am new to java script i dont no how to get cellid in other java script – Vishwanath.M Apr 18 '15 at 03:05
  • @Vishwanath In the same way you access it in `signal.js`. Think of it as a single large ` – Spencer Wieczorek Apr 18 '15 at 03:06
  • The OP posted script looks like NodeJS, not a webpage, based on `module.exports` and `require()` calls. – Norman Breau Apr 18 '15 at 03:07
  • @NormanBreau They can be used on a webpage (provided there is a proper server). The user can just use `require("signal.js")` for their file [as in this answer](http://stackoverflow.com/questions/4481058/load-and-execute-external-js-file-in-node-js-with-access-to-local-variables) if that's the case. – Spencer Wieczorek Apr 18 '15 at 03:17
0

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 ~

qianjiahao
  • 399
  • 1
  • 3
  • 10