Time:
Scotland: Fri, 11:11 pm
Rhode Island: Fri, 6:11 pm
Florida: Fri, 6:11 pm
New Mexico: Fri, 4:11 pm
California: Fri, 3:11 pm

Click here to visit Livelife365.com

Click here to visit nmdarts.com



Buy this Ad Space.

180px wide.

Please get in touch with KH@ if you are interested and make an offer.

CLICK HERE TO GET AUCTION BAR NOW
US$10 per year - Save $100s!
The Fabulously Unfair
WebX Auction Bar. For Ebay etc.
Ro-Sham-Bo the opposition. Laugh like Eric Cartman when you win! CLICK HERE NOW!


More information and sign-up.

WebXpertz Hosting.
Custom fit from $5pm. PHP/MySQL
You'll save money, we'll save money. Seems fair to me. Interested? If so Please PM me here and tell me what you need. Thanks!


Please click here for more information

For continued disscussion on this topic : Change a variable from a form


KH@
05-01-2002, 09:16 AM
I want to reset variables on a page without reloading the page.


This script sets a variable called "myVariable" then writes it to the page.


<p><script>
var myVariable = 'some text';
document.write(myVariable)
</script></p>

Now I want to change the variable so that all instances of it are rewritten with a new value. I want to change the variable using a form like the one below.

<form>
<input type="text" name="myVariable">
<input type="submit" value="change myVariable">
</form>

This is a piece of cake with serverside script but it requires reloading the page. I was hoping it can be done with JavaScript to save a trip to the server.

Can it? :confused:

diades
05-01-2002, 11:13 AM
Hi Keith

You don't, necessarily, need a form for this (you could use a link or anything that has a click event) but the easiest way to use a form with submitting is not to have a submit button, use an ordinary one.

<form>
<input type="text" name="myVariable">
<input type="button"
onclick="myVariable = 'I have been changed'" value="change myVariable">
</form>


If you need to have the onsubmit button then simply return false in the onsubmit for the form.

<form onsubmit="myVariable = 'I have been changed';return false">
<input type="text" name="myVariable">
<input type="submit" value="change myVariable">
</form>

KH@
05-01-2002, 12:14 PM
Hi Keith,

That is brilliant news. It will improve performance by miles. :D

Will it affect/change the variables in a linked .js file too?

KH@
05-01-2002, 12:39 PM
Hmmm. Something is still wrong I think.

I get an error message saying

OBJECT DOES NOT SUPPORT THIS PROPERTY OR METHOD

:confused:

diades
05-01-2002, 12:58 PM
Hi Keith,

What was the error caused by? What script are you using?

KH@
05-01-2002, 01:08 PM
<script>
var myVariable = 'London';
document.write(myVariable)
</script>

<form>
<input type="text" name="myVariable">
<input type="button"
onclick="myVariable = 'I have been changed'" value="change myVariable">
</form>

What I want to do say.. is type Manchester into the text box click the button and make the document.write above (and all others on the page) type out Manchester instead of London.

The script you gave me doesn't look like it will do that but it generates an error message anyway so I can't start to play with it.

diades
05-01-2002, 01:15 PM
Hi Keith

You will need to call a function to actually change the value and cause a re-write. It will be better to use innerHTML than doc.write (except for N4) wher you should use layes and write to that otherwise you will be clearing everything on the page everytime that you re-write.

KH@
05-01-2002, 01:27 PM
I only really need it for MSIE5.5 - 6.0 (and NS6 if possible).

I thought I would need a function but I had no idea how to make one rewrite the text value of a variable. I still don't :) but I will go look at some innerHTML stuff at webmonkey and see what it says.

KH@
05-01-2002, 02:25 PM
Can't seem to fins anything relevent.

Am I on the right track with this ? It doesn't cause an error but it doesn't change anything either. I think it's the red bit that's wrong.

<html>
<head>
<title>Untitled</title>
<script>
var town = 'London';
function fixTown(newtown) {
var town = newtown;
}
</script>
</head>

<body>
<script>
document.write(town);
</script>

<form name="form1" id="form1">
<input type="text" name="town" id="town">
<input type="button"
onclick="fixTown(document.form1.town.value);" value="change town">
</form>


</body>
</html>

diades
05-01-2002, 03:13 PM
Hi Keith

Try this, without the var:

<html>
<head>
<title>Untitled</title>
<script>
function fixTown(newtown) {
for(var i = 0;i < 3;i++){
document.getElementById("varP" + i).innerHTML = newtown
}
</script>
</head>
<body onload="fixTown('London') ">
<p id="varP0"></p>
<form name="form1" id="form1">
<input type="text" name="town" id="town">
<input type="button" onclick="fixTown(this.town.value);">
</form>
<p id="varP1"></p>
<p id="varP2"></p>
<p id="varP3"></p>
</body>
</html>

KH@
05-01-2002, 03:52 PM
Two errors, one easily fixed was function needed closing }

Other error is: 'this.town.value' is null or not an object

:bambooz:

I see you are looping to id the paras that need changing. There is only one change required within a paragraph. The paragraph writes two variables like this...

<p>Your location is : 'townvariable' : 'areavariable'</p>

All the rest (over 100) are in a linked .js file

see the #w4town_name# and #w4areaid# in this sample line of script from the .js file. These are coldFusion variables. I want to use JavaScript variables instead and be able to change them so the query string is altered when the popup window function is activated.

Menu1_1_4=new Array("Edit a directory page","javascript: newWindow = openWin('../edit_frame.cfm?w4town_name=#w4town_name#&w4areaid=#w4areaid#', 'editdirectory', 'width=640,height=440,toolbar=0,location=0,directories=0,status=0,menuBar=0,scrollBars=1,resizable=1 ' ); newWindow.focus()","",0);

What happens at present is: the visitor types a town name into a form textbox and selects the county it is in from a select box, then submits.

When submitted we do a quick run to the server and back to replace the CF variables with what the visitor chose so the query string reflects their new chosen location.

I want to cut out the trip to the server by replacing the CF variables with javscript variables and change them all from a javascript form without running to the server and back.

The single <P> contains the newly selected town and county to show/confirm the visitors new location.

diades
05-01-2002, 04:38 PM
Hi Keith

This tests out in IE6, I had to change some stuf so that it would run on my set up (functions, page names etc) but you should be able to do something with it

<html>
<head>
<title>Untitled</title>
<script>
function fixTown(frm) {
var w4town_name
var w4areaid
if(typeof(frm) == "string"){
w4town_name = frm
w4areaid = ""
}else{
w4town_name = frm.town.value
w4areaid = frm.area.value
}
for(var i = 0;i < 3;i++){
document.getElementById("varP" + i).innerHTML = w4town_name
}
Menu1_1_4 = new Array("Edit a directory page","newWindow = window.open(\"link.htm?w4town_name=" +
w4town_name + "&w4areaid=" + w4areaid +
"\", \"editdirectory\", \"width=640,height=440,toolbar=0,location=0,directories=0,status=0,menuBar=0,scrollBars=1,resizable=1\" ); newWindow.focus()",
"",0);
eval(Menu1_1_4[1])
return false
}
</script>
</head>
<body>
<p id="varP0"></p>
<form name="form1" id="form1" onsubmit="return fixTown(this)">
<input type="text" name="town" id="town">
<input type="text" name="area" id="area">
<input type="submit">
</form>
<p id="varP1"></p>
<p id="varP2"></p>
<p id="varP3"></p>
</body>
</html>

KH@
05-01-2002, 05:40 PM
Intriguing. But as usual I don't know how most of it works. I will experiment to see what is doing what.

Can you give me a few hints ? :)

Why this ? It checks to see of frm is a string ??? why ?

if(typeof(frm) == "string"){
w4town_name = frm
w4areaid = ""

and what does this do ? and do I need one for every menu array ?

eval(Menu1_1_4[1])
return false

The sample menu array is within the functions { }

Do I need to put all of them in there?
I mean does the function surround all the menu arrays in the in the .js file?

Anyway it will give me something to do and keep me out of trouble ;)

Thanks, I might be back later.

diades
05-01-2002, 05:54 PM
Why this ? It checks to see of frm is a string ??? why ?

if(typeof(frm) == "string"){
w4town_name = frm
w4areaid = ""

I originally ran the function from the onload of the page to give the variable a value to start with. this I sent in as a string so "frm" carried a string or an object depending upon when it was called. I then took out the initial call and just forgot to remove that part.

and what does this do ? and do I need one for every menu array ?
No, you can send that in as an argument, just replace the
"Menu1_1_4[1]" with the argumant name
eval(argumentname)

The sample menu array is within the functions { }
Do I need to put all of them in there?
I mean does the function surround all the menu arrays in the in the .js file?
It might be better, certainly more efficient as they would then only exist when required. Plus, the changes take effect immediately from the function.
Its always better to have local variables and little or no global variables if possible

KH@
05-01-2002, 06:15 PM
OK thanks, I will see if I can get it going after Voyager and StarGate have finished. :rolleyes:

diades
05-01-2002, 06:22 PM
Glad to see that you have your priorities:haha:

KH@
05-01-2002, 06:50 PM
Quite right!

However this is a performance tweak that can wait till 7pm. and Barcley is in this episode. (the funny guy from A-team and New Gen). I will include the tweak in the upgrade I'm working on. I'm sticking that portal I was playing with on the front off the main site.

Do you want to see? Just click my web4uk link below.

or to see the page I'm using the script for...

http://web4uk.tv/portal/portal_home.cfm

KH@
05-03-2002, 10:12 AM
I have tried the variable changing script you suggested but I am too dumb to make it work. In my defence I point out that it needs to be integrated into two .JS scripts (a config and a com) Whatever i try makes the menu the arrays help create disappear entirely.

Nevermind. I did a work around with two temp cookies and a frames page which is unconventional but works for what I want.

Basically I changed the CF variables to these text values xtownx and xidx then wrote out the links in the array as

frame.cfm?site=http.etc.html?town=xtownx etc

then on the frame.cfm page I ran a replace on the string "site" which substituted the cookie value.

<cfif isdefined("cookie.newtown")>
<cfset w4town_name = "#cookie.newtown#">
<cfset w4areaid = "#cookie.newarea#">
</cfif>

<cfif (site contains "xtownx") OR (site contains "xidx")>
<cfset site = #replace(site,"xtownx","#w4town_name#")#>
<cfset site = #replace(site,"xidx","#w4areaid#")#>

<frame name="thewindow" src="#site#" marginwidth="0" marginheight="0" scrolling="auto" frameborder="0">

<cfelse>
<frame name="thewindow" src="#site#" marginwidth="0" marginheight="0" scrolling="auto" frameborder="0">

</cfif>

and wrote the frame src with the site variable.


The upshot is that although I still have to run to the server, less than 1kb needs resending. Before it was 36k.

Anyway even though it's not very interesting I thought I'd let you know as you took the time to help with the script.

Thanks as always. :)