i have n no of js file say 1.js, 2.js, 3.js .......
all the three js contains one function say
function hello()
{
alert("In js file 1/2/3/...n"); // as and in which js
}
now i have another js file say x
in x we have var id and from somewhere i am getting id value
if id=1 the hello function of js1 should be called and alert button should popup In js file 1
if id=2 the hello function of js1 should be called and alert button should popup In js file 2
....
....
if id=15 the hello function of js15 should be called and alert button should popup In js file 15
if id=n the hello function of js'n' should be called and alert button should popup In js file n
how can this be done. any suggestion
Brijendra GautamPosted Mar 18, 2014, 9:03 AM
nishant ranjanPosted Mar 18, 2014, 6:40 AM
$(function()
{
var jsFile="Template_"+id;
jsFile:hello()
}
Cheers!!!!
nishant ranjanPosted Mar 18, 2014, 5:23 AM
my both js is in different folder should i have to pass full path of template_1 js in
var script = "Template_" + id +".js";
like
var script = "jsFolder/"+"Template_" + id +".js";
???
Brijendra GautamPosted Mar 18, 2014, 4:31 AM
This way jquery will dynamically load the required Js file and then call the Hello function from that js file.
nishant ranjanPosted Mar 18, 2014, 3:39 AM
JavaScript runtime error: Object doesn't support property or method 'Template_1.hello'
what i have used is
var id = "1";
var method_name = ".hello";
var method_prefix = "Template_";
var test= window[method_prefix + id + method_name]();
function name is hello()
js file name is Template_1
Jignesh TrivediPosted Mar 18, 2014, 3:32 AM
I think it will work for you.
nishant ranjanPosted Mar 18, 2014, 3:28 AM
but dont we have some way in js to call say if id=1
then hello function inside temp_1.js should be called
if id= 2 then hello function of temp_2.js should be called
............
Jignesh TrivediPosted Mar 18, 2014, 3:15 AM
hi,
you can also call js function dynamically...
function test(id)
{
var method_name = ".Hello";
var method_prefix = "Js";
// Call function:
window[method_prefix + id + method_name]();
}
Please refer
http://viralpatel.net/blogs/calling-javascript-function-from-string/
hope this will help you.
nishant ranjanPosted Mar 18, 2014, 3:09 AM
i need reference to that js file. actually the js file is saved as temp_1.js, temp_2.js and so on and each contains hello function. temp_ is common and 1,2,...will be same as id in x.js
like if we have id=1
then we will select js file as say
var abc=temp_'id'.hello(); where temp_id will refer to that js and its hello function will be called
can we call function say hello1() like this in x. js hello+"1"+"()"; is it possible to call function this way or any other way to call function with this situation.
Jignesh TrivediPosted Mar 18, 2014, 3:00 AM
Hi,
try may be following code can help you
Js1
------
var Js1 = new (function() {
this.Hello = function() {
alert('In js file 1');
};
})();
Js2
------
var Js2 = new (function() {
this.Hello = function() {
alert('In js file 2');
};
})();
soon.....
js -x function
function test(id)
{
if(id ==1)
Js1.Hello();
else if(id ==2)
Js2.Hello();
}