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'></iframe>";
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>";
}
Pranay Rana
177k37 gold badges244 silver badges267 bronze badges
asked Jul 12, 2010 at 5:10
rajesh
1,4333 gold badges17 silver badges23 bronze badges
-
No this will not work as you have written it.Jesse Dhillon– Jesse Dhillon2010年07月12日 05:15:43 +00:00Commented Jul 12, 2010 at 5:15
-
Could you clean up this code to only contain the pieces relevant to your question? Otherwise it's hard to guess what your question is. Also, where do you call add?nas– nas2010年07月12日 06:52:16 +00:00Commented Jul 12, 2010 at 6:52
1 Answer 1
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>";
}
};
answered Jul 12, 2010 at 10:20
gblazex
50.3k12 gold badges100 silver badges92 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-js