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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Stupid string/value problem 1

Status
Not open for further replies.

Sleidia

Technical User
May 4, 2001
1,284
FR

Hi guys :)

Can anyone remind me how to modify the following code in order to make alert(my_string) echo "my variable equals something now." ?

I don't get why my_string isn't updated when my_var changes :(

Code:
<SCRIPT LANGUAGE="JavaScript1.2">
<!-- 

var my_var = 'nothing';
var my_string = 'my variable equals ' + my_var + ' now.'

// End -->
</SCRIPT>

<a href="javascript:
my_var = 'something';
alert(my_string);
">test</a>

Thanks ;)
 
[tt]<a href="javascript:
my_var = 'something';
[red]my_string = 'my variable equals ' + my_var + ' now.';[/red]
alert(my_string);
">test</a>
[/tt]
my_var being updated, no reason my_string will automatically be.
 
HI Suji :)

Thanks but I absolutely need to keep the "test" button.
my_string has to be dynamically updated on a mouse click event.
 
If I say my_string being a memory location...blah blah, you know it all and it is unnecessary.

How about this kind of symbolic approach?
[tt]
var my_var = 'nothing';
var my_string[blue]_precompile = "'my variable equals ' + my_var + ' now.'"[/blue];
[/tt]
and then
[tt]
<a href="javascript:
my_var = 'something';
alert([blue]eval(my_string_precompile)[/blue]);
">test</a>
[/tt]
There is no need to prejudge eval(), just use it as necessity call.
 

Thanks alot :)

But in order to implement it within my script I need to know one more thing :

What if my_string becomes an array such like this :

Code:
my_string[0] = '...some code here...'
my_string[0] += '<input type=\"text\" name=\"form_name\" value=\"' + my_var + '\">';
my_string[0] += '...some code here...'

As you can see, I'd like to keep the single quotes so that I can keep doublequotes with the html code (because it's standard compliant).




 
It takes some get-use-to-it. Something like this.
[tt]
<SCRIPT LANGUAGE="JavaScript1.2">
<!--

var my_var = 'nothing';
var my_string=Array(1); //length 1 is just for illustration

my_string[0] = "'...some code here...";
my_string[0] += "<input type=\"text\" name=\"form_name\" value=\"' + my_var + '\">";
my_string[0] += "...some code here...'";

//alert(my_string[0]); //for debugging and to see what happens
//alert(eval(my_string[0])); //for debugging

// End -->
</SCRIPT>
[/tt]
And then...
[tt]
<a href="javascript:
my_var = 'something';
alert(eval(my_string[0]));
">test</a>
[/tt]
 

Well, I've just found out that eval() won't make it.
Too many problems arise with its use.

Is there another working method that doesn't use eval()?
 
eval() is just a proxy of last-minute-compilation. If it is too involved in adjusting the mind to write the string right, you can always set up a function with arguments including at least the latest value of my_var, and return the proper string just before using it (like alert() above) for instance assigning to some innerHTML etc...
 

Problem solved :)

I endend up using replace() incorporated in a function.
Thanks again for your time.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top