To access and use a variable or function dinamically, with Name from a String in JavaScript, just use the window
object, with this syntax:
window['variable_name'] // For function window['function_name'](parameters)Here is some examples, see the explanations in code.
var vr_name = 'vr1'; // string with name of a variable var vr1 = 'coursesweb.net/'; // uses variable with name stored as string in vr_name // window[vr_name] /window['vr1'] is the value of vr1 var site = 'http://'+ window[vr_name]; // Test document.write(site); // https://coursesweb.net/
// object with strings with variable names var vr_name = {'v1':'vr1', 'v2':'vr2'}; var vr1 = 'coursesweb.net/'; var vr2 = 'gamv.eu/'; // uses variable dinamically, with name stored as string in vr_name var i = 2; var site = 'https://'+ window[vr_name['v'+ i]]; // Test document.write(site); // https://gamv.eu/
function f1(a, b) { return a + b; } var f_name = 'f1'; // string with function name // uses function with name stored as string in f_name // window[f_name](parameters) calls the f1(parameters) var sum = window[f_name](12, 23); // Test document.write(sum); // 35
object['method_name'](parameters)
.
// object with strings with method names var methods = {'m1':'hi1', 'm2':'hi2'}; // object with two methods var obj = { 'hi1': function(name) { return 'Hello dear '+ name; }, 'hi2': function(name) { return 'Hola '+ name; } }; // uses object method with name stored as string in methods var hi = obj[methods['m1']]('ME'); // Test document.write(hi); // Hello dear ME
<table><tr> <th>Title 1</th> <th>Title 2</th> </tr></table>
.some_class { line-height: 150%; }
document.getElementById("id_button").onclick = function(){ window.open("http://coursesweb.net/"); }
$ar_dir = scandir("dir_name"); var_export($ar_dir);