Javascript Course

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.

Using variable with name stored into a string

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/

Using variable dinamically, with name stored into a string in object

// 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/

Calling function with name stored into a string

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

Calling object method with name stored in string into an object

- Use: 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

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag is used to add lists into <ul> and <ol> elements?
<dt> <dd> <li>
<ul>
 <li>http://coursesweb.net/html/</li>
 <li>http://coursesweb.net/css/</li>
</ul>
Which value of the "display" property creates a block box for the content and ads a bullet marker?
block list-item inline-block
.some_class {
  display: list-item;
}
Which instruction converts a JavaScript object into a JSON string.
JSON.parse() JSON.stringify eval()
var obj = {
 "courses": ["php", "javascript", "ajax"]
};
var jsonstr = JSON.stringify(obj);
alert(jsonstr);    // {"courses":["php","javascript","ajax"]}
Indicate the PHP class used to work with HTML and XML content in PHP.
stdClass PDO DOMDocument
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>';
$dochtml = new DOMDocument();
$dochtml->loadHTML($strhtml);
$elm = $dochtml->getElementById("dv1");
echo $elm->nodeValue;    // CoursesWeb.net
Using Variable and Function with Name from String in JavaScript

Last accessed pages

  1. PHP Simple HTML DOM Parser (12452)
  2. Disable button and Enable it after specified time (17532)
  3. Get Mime Type of file or string content in PHP (6229)
  4. PHP MySQL - using MySQLi (9669)
  5. Integer and Float value in Select with PDO from string to numeric (8658)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (477)
  2. CSS cursor property - Custom Cursors (81)
  3. The Mastery of Love (71)
  4. PHP-MySQL free course, online tutorials PHP MySQL code (63)
  5. CSS3 2D transforms (46)