1

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

4
  • There is not enough information here to answer this question Commented Apr 18, 2015 at 3: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? Commented Apr 18, 2015 at 3: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 Commented Apr 18, 2015 at 3: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. Commented Apr 20, 2015 at 5:34

2 Answers 2

1

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.

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

4 Comments

I aadded <script src="signal.js "></script> , but am new to java script i dont no how to get cellid in other java script
@Vishwanath In the same way you access it in signal.js. Think of it as a single large <script> with everything from signal.js at the top.
The OP posted script looks like NodeJS, not a webpage, based on module.exports and require() calls.
@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 if that's the case.
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 ~

Comments

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.