View Full Version : Convert String to Var

06-17-2004, 01:39 AM

I have this problem. I parsed the URL to grab a particular portion. That portion is a variable. That variable holds text that I want to print on screen. But the parsed variable is a string and when i write document.write(parsed) it only prints the string value not the variable value. How do I convert the parsed string into a variable name? Here's the code:

function getArticleName() {
var getName = window.location;
var position = getName.toString().indexOf("html");

// find the index value of html and then add 5 to get to beginning of article name
position = position + 5;

var articleTitle = getName.toString().substring(position, getName.toString().length);


The document.write at the end only prints the parsed URL portion, not the value of the variable I want. How do I resolve this?


06-17-2004, 01:52 AM
nevermind i figured it out. i just used eval(parsedstring) and it worked.

06-17-2004, 03:55 AM
eval is unnecessary. Search eval in this forum to see the evils of eval.
The better solution is to use the window object.


All global variables and functions are essentially member variables and functions of the window object. They can be access through the commonly used dot notation (e.g. window.str or window.myfunc()) and bracket notation (http://www.litotes.demon.co.uk/js_info/sq_brackets.html) (e.g. window['str'] or window['myfunc']()).