2

I want to access one function variable in another function,

My first function and variable

var max;
papaya.viewer.Viewer.prototype.pagination = function () {
 var max =  this.volume.header.imageDimensions.zDim;
}  

My second function

papaya.Container.fillContainerHTML = function(){
     // I want to access max value here
}

I want to access the max value to a second function,

I tried many ways to do but I can't able to make it.

Ashok Charu
  • 274
  • 2
  • 23

3 Answers3

0

Just declare max as a global variable, outside the functions, where it can be accessed by both, like so:

var max;
papaya.viewer.Viewer.prototype.pagination = function () {
 max =  this.volume.header.imageDimensions.zDim;
}
papaya.Container.fillContainerHTML = function(){
     // You can now access max value here
}

If it doesn't work, send an online demo so we can help.

Toufic Batache
  • 762
  • 10
  • 24
0

You can return this value and get from fillContainerHTML

papaya.viewer.Viewer.prototype.pagination = function () {
 return  this.volume.header.imageDimensions.zDim;
}

papaya.Container.fillContainerHTML = function(){
     var view = new papaya.viewer.Viewer();
    var a = view.pagination();
    console.log(a);

}
Tan Duong
  • 2,124
  • 1
  • 11
  • 17
0

The Best and easiest solution to your question

Hi. I had researched a lot about the question you are asking. I watched many tutorials, but they were really complicated. So once, I was working on a project. I didn't realise, but I used a variable created/edited in a function in another function. It was really simple. You need to create Local Storage to do this. The localStorage and sessionStorage properties allow to save key/value pairs in a web browser. It is Supported in almost every famous or known web browser like Google Chrome, Firefox, Opera, safari, Microsoft Edge etc.

Note: if you click Run code snippet button, the code won't work because Stack Overflow doesn't support Local Storage. So, go to another text editor like notepad, run the code, and see the result on any Browser (mentioned above). It will work.

// Creates a variable
var max = "Nothing";

function btn1() {
  // Makes a change to max variable
  var max = "Max variable changed";
  // Creates Local Storage on browser
  var setLocalStorage = localStorage.setItem("max", max);
}

function btn2() {
  var getLocalStorage = localStorage.getItem("max");
  document.ById("text").innerHTML = getLocalStorage;
}
<html>

<body>
  <p id="text"> Nothing </p>
  <button onclick="btn1()"> Make change </button>
  <button onclick="btn2()"> See change </button>
</body>

</html>
Soham Grover
  • 121
  • 6