How can I refer to the variable's name (literally the name of the variable)?
I have the following code:
var thisVariableName = 'path/filename.js';
var script = document.createElement('script');
script.id = **I WANT THE TEXT "thisVariableName" to be added HERE**
script.type = 'text/javascript';
script.src = thisVariableName;
So that if output script.id, I would get thisVariableName as such, not the value of the variable
AFTER SOME OF THE RESPONSES:
I want to systematize the addition of many script files. In the example below I add four files, but imagine I had to add 30+ files (please reserve yourself from asking why would I need to add 30+ files; after all this is the promise of computing)
As of now, the only way I know how to get this done is to create two separate arrays; array 1 for the files' name string, and array 2 for the files' paths, or ALTERNATIVELLY, as Felix Kling SUGGESTS create an object per each variable:
var jQueryPath = 'js/jquery-1.5.1.js';
var jQueryUICore = 'js/jquery.ui.core.js';
var jQueryUIWidget = 'js/jquery.ui.widget.js';
var jQueryUITabs = 'js/jquery.ui.tabs.js';
var fileNames = new Array ('jQueryPath','jQueryUICore','jQueryUIWidget','jQueryUITabs');
var filePaths = new Array (jQueryPath,jQueryUICore,jQueryUIWidget,jQueryUITabs);
var head = document.getElementsByTagName("head")[0];
for (var i=0;i<4;i++){
var script = document.createElement('script');
script.setAttribute('id',fileNames[i]);
script.setAttribute('type','text/javascript');
script.setAttribute('src', filePaths[i]);
head.appendChild(script);
}
So whether I create arrays to hold the names and the values of the variables, or create an object, it requires individual creation of both attributes per file.
I was hoping I could reference the literal variables' names of an array composed of the variables; i.e:
var onlyOneArray = new Array (jQueryPath,jQueryUICore,jQueryUIWidget,jQueryUITabs);
for (var i=0;i<4;i++){
var script = document.createElement('script');
script.setAttribute('id',**onlyOneArra[i] the NAME**);
script.setAttribute('type','text/javascript');
script.setAttribute('src', onlyOneArray[i]);
head.appendChild(script);
}