Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Spliting the arg's 3

Status
Not open for further replies.

Splittle

ISP
Jun 21, 2005
26
GB
Hello


i've rewrote this code over but i can't get it to work (still), i have a function with arguments likes this...

function getdata(tt,num){

var s=tt+num;

alert(s); //for testing // and works fine up to this point.

var listdata= s.split("^"); // <-- something wrong here

for some reason s doesn't equal what it's meant to.


the function is call by onclicks .. onclick=getdata("data","1");

also data1 is in a js file as

var data1="info^info";


do i have to use the toString() on it ?

any help would be good,
thanks
 

There is a difference between alerting the contents of a variable:
Code:
alert(s); // this is alerting the string "data1"
and alerting the contents of a variable that is named by a string contained in another variable.

I think what you are looking for here is:
Code:
alert(eval(s)); // alerts the contents of the data1 variable

Here is a working example:
Code:
<script type="text/javascript">
var data1 = "Real variable contents";
var myFragmentA = "data";
var myFragmentB= "1";
var myString = myFragmentA + myFragmentB;
alert(myString); // "data1"
alert(eval(myString)); // "Real variable contents"
</script>

Cheers,
Jeff
 
thanks Jeff

eval(s).split("^");

works great ..

star*
 
If you want to do the same thing, but without the use of eval, you can use:

Code:
alert(window[s].split('^'));

I don't know enough about your code to know whether using eval will slow you down too much (it does have a massive overhead compared to other methods), so you may choose to stick with it. But be aware that there are very few occasions that you really have to use eval - and this isn't one of them.

Hope this helps,
Dan

[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 
hhmmm, both of these work, now i'm confused as to which to use

what's the pro's and con's of each ?

and thanks for that i've never seen the window[] thing before
i'm new to javascript but i have seen eval() before, not should what it actually does, but still.

thanks both.
 
If eval works for you, and you don't have any problem with a slight delay (i.e. you're not running in a tight loop), or you prefer using code that you understand, then run with it.

If you're running in a tight loop, or want to make your code as quick as possible, then avoid eval like the plague.

Dan

[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 
no it's not a tight loop, it's just to make the initial split
but if windows[] is faster and works with all the browsers
then i may aswell use it.
 
window[]**

i really should use the "preview post" button.
 
BRPS: I've been coding JavaScript for almost as long as JavaScript has been around and I've never seen that usage. A star for you! Thanks!


Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
It's a good little trick to remember. The strange thing is, I don't remember where I first saw it.

Basically, any global items - be they variables or functions - can be accessed via the window object using the syntax:

Code:
window['someName']

So the following pairs are all identical:

Code:
var a = 2;
window['a'] = 2;

foo();
window['foo']();

alert('some text');
window['alert']('some text');

I've found it especially helpful when it comes to objects calling themselves from a setTimeout. if any object is aware of its own name, you can do things like:

Code:
window['yourObjName'].callBackMethod();

instead of using eval.

Good to know for a rainy day ;o)

Dan

[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 

I've never seen that FAQ before. Good to know about, however!

Dan


[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top