Calling one parameter in another function?
Actually i want to take url and id from one fn to another whether this method will work.
function addElement(url, id) {
alert(url);
var main = document.getElementById('mainwidget');
main.innerHTML = "<iframe src=" + url + " align='left' height='1060px' width='576px' scrolling='no' frameborder='0' id='lodex'></i开发者_开发百科frame>";
alert("google");
addUrl(url, id);
}
function add() {
addUrl(url, id)
alert("id" + id);
document.getElementById("Listsample").innerHTML = "<a href='#' onclick='addUrl(" + url + ");' id='cricket' tabindex='1' name='cricket'>" + id + "</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>";
}
};
精彩评论