Richard Nicson
Richard Nicson

Reputation: 147

Take variable values and put them in another file

I'm coding the game blockly, I have a variable called lineCount which counts the number of line breaks. however this variable is in a file called lib-dialog.js. When I insert the value of this variable with innerHTML I can get the value of lines by creating a div in the soy.js file (File by which I need to treat the result) But I need this value in a variable to put an if(lines == 6) { }

// Add the user's code.
  if (BlocklyGames.workspace) {
    var linesText = document.getElementById('dialogLinesText');
    linesText.textContent = '';
    // Line produces warning when compiling Puzzle since there is no JavaScript
    // generator.  But this function is never called in Puzzle, so no matter.
    var code = Blockly.JavaScript.workspaceToCode(BlocklyGames.workspace);
    code = BlocklyInterface.stripCode(code);
    var noComments = code.replace(/\/\/[^\n]*/g, '');  // Inline comments.
    noComments = noComments.replace(/\/\*.*\*\//g, '');  /* Block comments. */
    noComments = noComments.replace(/[ \t]+\n/g, '\n');  // Trailing spaces.
    noComments = noComments.replace(/\n+/g, '\n');  // Blank lines.
    noComments = noComments.trim();
    var lineCount = noComments.split('\n').length;
    var pre = document.getElementById('containerCode');
    pre.textContent = code;
    if (typeof prettyPrintOne == 'function') {
      code = pre.innerHTML;
      code = prettyPrintOne(code, 'js');
      pre.innerHTML = code;
    }
    if (lineCount == 1) {
      var text = BlocklyGames.getMsg('Games_linesOfCode1');
    } else {
      var text = BlocklyGames.getMsg('Games_linesOfCode2')
          .replace('%1', String(lineCount));
    }
    linesText.appendChild(document.createTextNode(text));

    document.getElementById("contarBloco").innerHTML = lineCount;
    var contandoBloco = lineCount;
  }

I need to take the variable lineCount and put its value in another js.but I'm only managing to insert it into a div with innerHTML

Upvotes: 0

Views: 139

Answers (2)

Suraj
Suraj

Reputation: 61

it is better you use localstorage . Set the value of localcount in the local storage and get wherever you want

var lineCount = noComments.split('\n').length;

 localStorage.setItem("lineCount", lineCount); // in first file

 var count = localStorage.getItem("lineCount")  // in second file

with this logic you will get the value but it will be string then for that you either use directly string or convert into integer using parseInt method

parseInt(count);

may be it will help . Thanks

Upvotes: 1

Beweelam
Beweelam

Reputation: 1126

I don't know if your Javascript files are modules, if so, you can return a function in your lib-dialog.js page and return it.

Example

lib-dialog.js =

let Dialog = (function() {

   function SetLineCountVariable(LocalLineCount){
       LineCount = LocalLineCount;
   }

   return SetLineCountVariable

})();

And in your soy.js file

let Soy = (function() {

   Dialog.SetLineCountVariable(6);

})();

And do not forgot to call your JS file in order in your HTML page

Another way, if you only want the variable result in your another JS file, in your lib-dialog.js, show the result of LineCount in html tag and get it in your another JS file with document.getElementById

Upvotes: 0

Related Questions