Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Answer

Post Timeline

added 2 characters in body
Source Link
gblazex
  • 50.3k
  • 12
  • 100
  • 92

You need to share the information somehow, because variables are only available in their function's scope. The simplest way is to define global variables.

var url, id; // global
function addElement(_url, _id) {
 url = _url;
 id = _id;
 alert(url);
 var main = document.getElementById('mainwidget');
 main.innerHTML = "<iframe>...</iframe>";
 alert("google");
 addUrl(url, id);
},
function add() {
 addUrl(url, id)
 alert("id" + id);
 document.getElementById("Listsample").innerHTML = "<a>...</a>";
}

Note that they don't need to be global. You can wrap this module into its own scope

(function() {
/// code goes here
})();

Or you can also organize the whole code into an object (encapsulation).

var Adderaddy = {
 url: null, 
 id: null,
 
 addElement: function(url, id) {
 this.url = url;
 this.id = id;
 alert(url);
 var main = document.getElementById('mainwidget');
 main.innerHTML = "<iframe>...</iframe>";
 alert("google");
 addUrl(url, id);
 },

 add: function() {
 addUrl(this.url, this.id);
 alert("id" + this.id);
 document.getElementById("Listsample").innerHTML = "<a>...</a>";
 }
};

You need to share the information somehow, because variables are only available in their function's scope. The simplest way is to define global variables.

var url, id; // global
function addElement(_url, _id) {
 url = _url;
 id = _id;
 alert(url);
 var main = document.getElementById('mainwidget');
 main.innerHTML = "<iframe>...</iframe>";
 alert("google");
 addUrl(url, id);
},
function add() {
 addUrl(url, id)
 alert("id" + id);
 document.getElementById("Listsample").innerHTML = "<a>...</a>";
}

Note that they don't need to be global. You can wrap this module into its own scope

(function() {
/// code goes here
})();

Or you can also organize the whole code into an object (encapsulation).

var Adder = {
 url: null, 
 id: null,
 
 addElement: function(url, id) {
 this.url = url;
 this.id = id;
 alert(url);
 var main = document.getElementById('mainwidget');
 main.innerHTML = "<iframe>...</iframe>";
 alert("google");
 addUrl(url, id);
 }
 add: function() {
 addUrl(this.url, this.id);
 alert("id" + this.id);
 document.getElementById("Listsample").innerHTML = "<a>...</a>";
 }
};

You need to share the information somehow, because variables are only available in their function's scope. The simplest way is to define global variables.

var url, id; // global
function addElement(_url, _id) {
 url = _url;
 id = _id;
 alert(url);
 var main = document.getElementById('mainwidget');
 main.innerHTML = "<iframe>...</iframe>";
 alert("google");
 addUrl(url, id);
},
function add() {
 addUrl(url, id)
 alert("id" + id);
 document.getElementById("Listsample").innerHTML = "<a>...</a>";
}

Note that they don't need to be global. You can wrap this module into its own scope

(function() {
/// code goes here
})();

Or you can also organize the whole code into an object (encapsulation).

var addy = {
 url: null, 
 id: null,
 
 addElement: function(url, id) {
 this.url = url;
 this.id = id;
 alert(url);
 var main = document.getElementById('mainwidget');
 main.innerHTML = "<iframe>...</iframe>";
 alert("google");
 addUrl(url, id);
 },

 add: function() {
 addUrl(this.url, this.id);
 alert("id" + this.id);
 document.getElementById("Listsample").innerHTML = "<a>...</a>";
 }
};

Source Link
gblazex
  • 50.3k
  • 12
  • 100
  • 92

You need to share the information somehow, because variables are only available in their function's scope. The simplest way is to define global variables.

var url, id; // global
function addElement(_url, _id) {
 url = _url;
 id = _id;
 alert(url);
 var main = document.getElementById('mainwidget');
 main.innerHTML = "<iframe>...</iframe>";
 alert("google");
 addUrl(url, id);
},
function add() {
 addUrl(url, id)
 alert("id" + id);
 document.getElementById("Listsample").innerHTML = "<a>...</a>";
}

Note that they don't need to be global. You can wrap this module into its own scope

(function() {
/// code goes here
})();

Or you can also organize the whole code into an object (encapsulation).

var Adder = {
 url: null, 
 id: null,
 
 addElement: function(url, id) {
 this.url = url;
 this.id = id;
 alert(url);
 var main = document.getElementById('mainwidget');
 main.innerHTML = "<iframe>...</iframe>";
 alert("google");
 addUrl(url, id);
 }
 add: function() {
 addUrl(this.url, this.id);
 alert("id" + this.id);
 document.getElementById("Listsample").innerHTML = "<a>...</a>";
 }
};

lang-js

AltStyle によって変換されたページ (->オリジナル) /