/    Sign up×
Community /Pin to ProfileBookmark

Stock Pricing Points #2

Assistance desired with two issues, please:
ISSUE #1 – cookie restore function is not working properly upon initial load (works well as long as browser remains open)
ISSUE #2 – how does one establish a variable in a link, the value of that variable which resides in a form?
Please advise. Thanks!

SEARCH on keyword: ISSUE in the code below

[CODE]<HTML>

see current working version below

</HTML>[/CODE]

to post a comment
JavaScript

18 Comments(s)

Copy linkTweet thisAlerts:
@Jonny_LangAug 11.2005 — .....
Copy linkTweet thisAlerts:
@katydidauthorAug 11.2005 — J Lang, Thanks.

Code as written and originally presented above successfully writes cookie with expiration date to user's disk.

The issue is that the cookie data is not successfully restored to the web page form whenever the page is initially loaded.

Any additional thoughts would be appreciated.
Copy linkTweet thisAlerts:
@Jonny_LangAug 11.2005 — .....
Copy linkTweet thisAlerts:
@katydidauthorAug 11.2005 — J Lang, Thanks.

I tried result(); as both FIRST and LAST line in the "restoreValues" function as well as the following approach:

window.onload=result;

window.onbeforeunload=SaveForm;

with restoreValues(); as the FIRST line of the "result" function and still no success, even placed the code for restoreValues() ahead of result() in the <Script> section.

Conjecture:

Given the form has six elements, yet the cookie has five elements, does this approach compromise the restoreValues function?

Have I created a problem by combining two different input types in the same cookie?

Form elements are ticker,:buyprice,profittarget,profityn,profityn,sellprice,

(note: all are "type=text" except profityn is "type=radio")

Cookie elements are ticker,buyprice,profittarget,profityn,sellprice,
Copy linkTweet thisAlerts:
@Jonny_LangAug 11.2005 — .....
Copy linkTweet thisAlerts:
@katydidauthorAug 11.2005 — J Lang, Thanks!

Will the approaches recommended above work if the [U]form[/U] elements are in the order of text, text, text, radio, radio, text?

Or should I sacrifice page appearance and but the radio input at the end of the form?
Copy linkTweet thisAlerts:
@Jonny_LangAug 12.2005 — .....
Copy linkTweet thisAlerts:
@katydidauthorAug 12.2005 — J Lang, Thanks.

I've gone at this several ways, focusing just on the "text" values, ignoring the "radio" values to keep it simple. Have saved cookie with and without names for each of the values.

This aspect of the code works. result() function and IMG retrieve work, too, though subsequent to manual load of data into the form after browser opens.

Using your recommended approach for the retrieve as well as a couple of others, my problem continues to be during the cookie retrieve operation.

Error occurs during

window.onload=restoreValues();

at the following line of the restoreValues() function:

isForm = document.forms.PTform01;

Error: 'isForm' is null or not an object

Code: 0

alert (isForm); returns "undefined"

Your suggestions would be appreciated.
Copy linkTweet thisAlerts:
@Jonny_LangAug 13.2005 — katydid:

It should be:

window.onload=restoreValues;

Not,

window.onload=restoreValues();


You should have this line in the saveValues function as well:

isForm = document.forms.PTform01;


If it's working in one and not the other, check for a case difference. JavaScript is case sensitive.


I'm sorry, but I've decided to stop particpating here. The constant harrassment by a particular individual makes is not worth my time.

I've deleted all my posts in this thread.

One would think that volunteering should be somewhat rewarding, maybe even fun. Not here. Not for me, anyway. It's constant, gratuitous confrontation. There is a "favored" group, that may write what they wish, when they wish, no matter how insolent, without consequence.

Fine.

I wish you the best, and good luck with the rest of your project.

Take care.
Copy linkTweet thisAlerts:
@katydidauthorAug 13.2005 — Thanks for all your assistance; you've been most patient.

Best of luck in your future endeavors!
Copy linkTweet thisAlerts:
@Jonny_LangAug 13.2005 — Corrected code posted below.
Copy linkTweet thisAlerts:
@Jonny_LangAug 13.2005 — katydid:

In my haste this morning I failed to discover two errors. And I also corrected for the NaN display in the sellprice box.

[CODE]<HTML>
<Head>
<Style>
input {text-align:center}
</Style>

<!-- SEARCH on keyword: ISSUE -->

<Script Language=JavaScript>

<!-- set cookie to be able to save and restore PTform01, PTform02 data -->

var saveString = "";

var expDate = new Date();
expDate.setTime(expDate.getTime()+365*24*60*60*1000); // one year

function setCookie(isName,isValue,dExpires){

document.cookie = isName+"="+isValue+";expires="+dExpires.toGMTString();
}


function getCookie(isName){

cookieStr = document.cookie;
startSlice = cookieStr.indexOf(isName+"=");
if (startSlice == -1){return false}
endSlice = cookieStr.indexOf(";",startSlice+1)
if (endSlice == -1){endSlice = cookieStr.length}
isData = cookieStr.substring(startSlice,endSlice)
isValue = isData.substring(isData.indexOf("=")+1,isData.length);
return isValue;
}

function SaveForm(){

isForm = document.forms.PTform01;
saveString = "";
for (i=0; i<isForm.length; i++)
if (isForm[i].type == "text")
{
saveString += isForm[i].value+":";
}
//alert(saveString);
setCookie('PTform01Data',saveString,expDate)
if (isForm.profityn[0].checked){setCookie('radio1',1,expDate)}
else {setCookie('radio1',0,expDate)}
}

function restoreValues(){

nValues = getCookie('PTform01Data');
n =0;
isForm = document.forms.PTform01;
if (nValues)
{
isText = nValues.split(":");
for (i=0; i<isForm.length; i++)
{
if (isForm[i].type == "text")
{
isForm[i].value = isText[n++];
}
}
radioState = getCookie('radio1');
if (radioState == '1'){isForm.profityn[0].checked = true}
else {isForm.profityn[1].checked = true}
}

result();
}


<!-- calculate sell price for each stock -->

function result(){

<!-- calculate sell price for each stock -->

var basis1 = document.PTform01.buyprice.value;
var gain1 = document.PTform01.profittarget.value;
var pt1 = document.PTform01.profityn[0].checked;

if (pt1){
sale1 = basis1 * (1 + gain1 / 100);
document.PTform01.sellprice.value = sale1.toFixed(2)}
else
{
var tmp = document.PTform01.sellprice.value;
if (isNaN(tmp) || tmp == ""){document.PTform01.sellprice.value = "N/A"}
else {document.PTform01.sellprice.value = parseFloat(tmp).toFixed(2)}
}

var basis2 = document.PTform02.buyprice.value;
var gain2 = document.PTform02.profittarget.value;
var pt2 = document.PTform02.profityn[0].checked;

if (pt2){
sale2 = basis2 * (1 + gain2 / 100);
document.PTform02.sellprice.value = sale2.toFixed(2)}
else
{
tmp = document.PTform02.sellprice.value;
if (isNaN(tmp) || tmp == ""){document.PTform02.sellprice.value = "N/A"}
else {document.PTform02.sellprice.value = parseFloat(tmp).toFixed(2)}
}

<!-- retrieve price chart for each stock -->

var ticker01 = document.PTform01.ticker.value
var buyprice01 = document.PTform01.buyprice.value
var sellprice01 = document.PTform01.sellprice.value
var vFileName="http://stockcharts.com/def/servlet/SharpChartv05.ServletDriver?chart=" + ticker01 + ",uu[s,a]daclyyay[de][pa" + buyprice01 + "!a" + sellprice01 + "!f]"
document.getElementById('ticker01ImageA').src = vFileName;

var ticker01 = document.PTform01.ticker.value
var buyprice01 = document.PTform01.buyprice.value
var sellprice01 = document.PTform01.sellprice.value
var vFileName="http://stockcharts.com/def/servlet/SharpChartv05.ServletDriver?chart=" + ticker01 + ",uu[s,a]daclyyay[db][pa" + buyprice01 + "!a" + sellprice01 + "!f]"
document.getElementById('ticker01ImageB').src = vFileName;

var ticker02 = document.PTform02.ticker.value
var buyprice02 = document.PTform02.buyprice.value
var sellprice02 = document.PTform02.sellprice.value
var vFileName="http://stockcharts.com/def/servlet/SharpChartv05.ServletDriver?chart=" + ticker02 + ",uu[s,a]daclyyay[de][pa" + buyprice02 + "!a" + sellprice02 + "!f]"
document.getElementById('ticker02ImageA').src = vFileName;

var ticker02 = document.PTform02.ticker.value
var buyprice02 = document.PTform02.buyprice.value
var sellprice02 = document.PTform02.sellprice.value
var vFileName="http://stockcharts.com/def/servlet/SharpChartv05.ServletDriver?chart=" + ticker02 + ",uu[s,a]daclyyay[db][pa" + buyprice02 + "!a" + sellprice02 + "!f]"
document.getElementById('ticker02ImageB').src = vFileName;

SaveForm();

}

window.onload=restoreValues;
window.onbeforeunload=SaveForm;

</Script>
</Head>

<Body>
<p><table
border=0 width=100% cellpadding=4 cellspacing=0><tr bgcolor=6699cc><td><font face=arial><b><big>Base Design</big></b></font></td><td align=right><table border=0 cellpadding=2 bgcolor=dcdcdc><tr><td><font face=arial size=-1><b>Price Targets</font></a></td></tr></table></td></tr></table><table border=0 cellpadding=2>
<BR>

<!-- first stock entry form and price chart -->
<form name=PTform01>
<table border=1>
<tr>
<td align=center><font face="arial, helvetica" size="-1"><b>Ticker</b></td>
<td align=center><font face="arial, helvetica" size="-1"><b>Buy Price</b></td>
<td align=center><font face="arial, helvetica" size="-1"><b>% Profit Targ</b></td>
<td align=center><font face="arial, helvetica" size="-1"><b>Use PT?</b></td>
<td align=center><font face="arial, helvetica" size="-1"><b>Sell Price</b></td>
</tr>
<tr>
<td align=center><input type=text name=ticker size=8></td>
<td align=center><input type=text name=buyprice size=8></td>
<td align=center><input type=text name=profittarget size=8></td>
<td align=center><input type=radio name=profityn value="1" size=8 checked>yes
<br><input type=radio name=profityn value="-1" size=8>no</td>
<td align=center><input type=text name=sellprice size=8></td>
<td align=center><input type=button value="Calc!" name=solve size=8 onClick="result()"></td>
</tr>
</table>
</form>

<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tr>

<!-- ISSUE #2A -->
<!-- how does one establish a variable represented by "ARG" in the line below -->
<!-- equal to whatever value is in "document.PTform01.ticker.value" -->

<td align="left" width="2%"><A HREF="http://moneycentral.msn.com/investor/research/profile.asp?Symbol=ARG"><b>Profile</b></A></td>
</tr>
</table>

<IMG ID='ticker01ImageA' SRC=''>
<IMG ID='ticker01ImageB' SRC=''>
<BR>
<BR>

<!-- second stock entry form and price chart -->
<form name=PTform02>
<table border=1>
<tr>
<td align=center><font face="arial, helvetica" size="-1"><b>Ticker</b></td>
<td align=center><font face="arial, helvetica" size="-1"><b>Buy Price</b></td>
<td align=center><font face="arial, helvetica" size="-1"><b>% Profit Targ</b></td>
<td align=center><font face="arial, helvetica" size="-1"><b>Use PT?</b></td>
<td align=center><font face="arial, helvetica" size="-1"><b>Sell Price</b></td>
</tr>
<tr>
<td align=center><input type=text name=ticker value="NSM" size=8></td>
<td align=center><input type=text name=buyprice value=12.50 size=8></td>
<td align=center><input type=text name=profittarget value=50 size=8></td>
<td align=center><input type=radio name=profityn value="1" size=8 checked>yes
<br><input type=radio name=profityn value="-1" size=8>no</td>
<td align=center><input type=text name=sellprice size=8></td>
<td align=center><input type=button value="Calc!" name=solve size=8 onClick="result()"></td>
</tr>
</table>
</form>

<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tr>

<!-- ISSUE #2B -->
<!-- how does one establish a variable represented by "NSM" in the line below -->
<!-- equal to whatever value is in "document.PTform02.ticker.value" -->

<td align="left" width="2%"><A HREF="http://moneycentral.msn.com/investor/research/profile.asp?Symbol=NSM"><b>Profile</b></A></td>
</tr>
</table>

<IMG ID='Ticker02ImageA' SRC=''>
<IMG ID='Ticker02ImageB' SRC=''>
<BR>
<BR>

</form>

</Body>
</HTML>[/CODE]
Copy linkTweet thisAlerts:
@Jonny_LangAug 13.2005 — katydid:

I don't know if you are going to have a default ticker in the forms, but if not, I noticed that, initially, the stock chart images will display the red x error.

Use the following to hide error images:

[CODE]<HTML>
<Head>
<Script Language=JavaScript>

function notFound(ID){

document.getElementById(ID).style.display = 'none';

}
</Script>
</Head>
<Body>
<IMG Src='A1.jpg' id=1 onerror="notFound(this.id)">
</Body>
</HTML>[/CODE]


And to unhide it:
[CODE]
document.getElementById('ticker01ImageA').style.display = 'inline';
document.getElementById('ticker01ImageA').src = vFileName;[/CODE]


Lastly:

Using one cookie for both types of data:

[CODE]function SaveForm(){

isForm = document.forms.PTform01;
saveString = "";
for (i=0; i<isForm.length; i++)
if (isForm[i].type == "text")
{
saveString += isForm[i].value+":";
}
ptYN = isForm.profityn[0].checked;
if (ptYN){saveString += 'y'}
else {saveString += 'n'}
//alert(saveString)
setCookie('PTform01Data',saveString,expDate);
}

function restoreValues(){

nValues = getCookie('PTform01Data');
n = 0;
isForm = document.forms.PTform01;
if (nValues)
{
isText = nValues.split(":");
for (i=0; i<isForm.length; i++)
{
if (isForm[i].type == "text")
{
isForm[i].value = isText[n++];
}
}

radioState = isText[n++];
if (radioState == 'y'){isForm.profityn[0].checked = true}
else {isForm.profityn[1].checked = true}
}

result();
}[/CODE]
Copy linkTweet thisAlerts:
@katydidauthorAug 15.2005 — J Lang, Thanks again.

1) default ticker - good suggestion, went ahead and assigned vales for both forms

2) I, too, had arrived at a one cookie solution. It works in the code below, but is there any flaw in the approach?

3) this version incorporates loop for appropriate functions to handle multiple forms

ISSUE 2 (keyword search for ISSUE in code below)

I've not been successful in building HREF link which is tied to the ticker in each form.

The current link for "Profile" works with javascript in the <BODY> but I have failed to conjure a means to place in the <HEAD>.

NOTE: this link needs to open in the [U]SAME window[/U] as the main page, NOT a new window.

Any suggestions would be appreciated.


[CODE]<HTML>
<Head>
<Style>
input {text-align:center}
</Style>

<Script Language=JavaScript>

<!-- set cookie to save and restore PTformxx data -->

function getCookie(isName){

cookieStr = document.cookie;
startSlice = cookieStr.indexOf(isName+"=");
if (startSlice == -1){return false}
endSlice = cookieStr.indexOf(";",startSlice+1)
if (endSlice == -1){endSlice = cookieStr.length}
isData = cookieStr.substring(startSlice,endSlice)
isValue = isData.substring(isData.indexOf("=")+1,isData.length);
return isValue;
}

function SaveForm(){

var numberForms = document.forms.length;
var formIndex;
for (formIndex = 0; formIndex < numberForms; formIndex++)
{
isForm = document.forms[formIndex];
savetextString = "";
saveradioString = "";
nText = isForm.getElementsByTagName('input');
for (i=0; i<isForm.length; i++ )
{
if (nText[i].type == "text")
{savetextString += nText[i].name + "," + nText[i].value + ":" }
}

if (isForm.profityn[0].checked){profitIdx = 0}
else {profitIdx = 1};
saveradioString = "profityn" + "," + profitIdx + ":" ; // radio state

var date = new Date();
date.setTime(date.getTime()+(10*365*24*60*60*1000)); // ten years
var expires = "expires="+date.toGMTString();

document.cookie = isForm.name + "Data=" + savetextString + saveradioString + " " + ";" + expires;
}
}

function restoreValues(){

var numberForms = document.forms.length;
var formIndex;
for (formIndex = 0; formIndex < numberForms; formIndex++)
{
isForm = document.forms[formIndex];
isCookie = isForm.name + "Data";
nValues = getCookie(isCookie);
n = 0;
if (nValues)
{
isText = nValues.split(":");
for (i=0; i<isForm.length; i++)
{
if (isForm[i].type == "text")
{
currText = isText[n++].split(",");
isForm[i].value = currText[1];
}
}
profitynText = isText[4].split(",");
profitIdx = profitynText[1];
if(profitIdx){isForm.profityn[profitIdx].checked = true};
}
}
result();
}


function result(){

var numberForms = document.forms.length;
var formIndex;
for (formIndex = 0; formIndex < numberForms; formIndex++)
{
isForm = document.forms[formIndex];

<!-- calculate sell price for each stock -->

var basis = isForm.buyprice.value;
var gain = isForm.profittarget.value;
var ptset = isForm.profityn[0].checked;

if (ptset) {sale = basis * (1 + gain / 100);
isForm.sellprice.value = sale.toFixed(2)}
else {var tmp = isForm.sellprice.value;
isForm.sellprice.value = parseFloat(tmp).toFixed(2);}

<!-- retrieve price charts for each stock -->

var ticker = document.forms[formIndex].ticker.value
var buyprice = document.forms[formIndex].buyprice.value
var sellprice = document.forms[formIndex].sellprice.value

var vFileName="http://?chart=" + ticker + ",uu[s,a]daclyyay[de][pa" + buyprice + "!a" + sellprice + "!f]"
var vImageName = document.forms[formIndex].name + "ImageA";
document.getElementById(vImageName).src = vFileName;

var vFileName="http://?chart=" + ticker + ",uu[s,a]daclyyay[db][pa" + buyprice + "!a" + sellprice + "!f]"
var vImageName = document.forms[formIndex].name + "ImageB";
document.getElementById(vImageName).src = vFileName;
}
SaveForm();
}

window.onload=restoreValues;
window.onbeforeunload=SaveForm;

</Script>
</Head>

<Body>
<p><table
border=0 width=100% cellpadding=4 cellspacing=0><tr bgcolor=6699cc><td><font face=arial><b><big>Base Design</big></b></font></td><td align=right><table border=0 cellpadding=2 bgcolor=dcdcdc><tr><td><font face=arial size=-1><b>Price Targets</font></a></td></tr></table></td></tr></table><table border=0 cellpadding=2>
<BR>

<!-- first stock entry form and price chart -->
<form name=PTform01>
<table border=1>
<tr>
<td align=center><font face="arial, helvetica" size="-1"><b>Ticker</b></td>
<td align=center><font face="arial, helvetica" size="-1" color="0000FF"><b>Buy Price</b></td>
<td align=center><font face="arial, helvetica" size="-1"><b>% Profit Targ</b></td>
<td align=center><font face="arial, helvetica" size="-1"><b>Use PT?</b></td>
<td align=center><font face="arial, helvetica" size="-1" color="FF0000"><b>Sell Price</b></td>
</tr>
<tr>
<td align=center><input type=text name=ticker value="CVX" size=8></td>
<td align=center><input type=text name=buyprice value= 47.00 size=8></td>
<td align=center><input type=text name=profittarget value=30 size=8></td>
<td align=center><input type=radio name=profityn value="1" size=8 checked>yes
<br><input type=radio name=profityn value="-1" size=8>no</td>
<td align=center><input type=text name=sellprice size=8></td>
<td align=center><input type=button value="Calc!" name=solve size=8 onClick="result()"></td>
</tr>
</table>
</form>

<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tr>

<!-- ISSUE #2 -->
<!-- http://?Symbol = "document.PTform01.ticker.value" -->

<Script Language=JavaScript>
<!--
var ticker01 = document.PTform01.ticker.value
document.write('<A HREF="http://?Symbol=' + ticker01 + '"><b>Profile</b></A></td>')
//-->
</Script>

</tr>
</table>

<IMG ID='PTform01ImageA' SRC=''>
<IMG ID='PTform01ImageB' SRC=''>
<BR>
<BR>

<!-- second stock entry form and price chart -->
<form name=PTform02>
<table border=1>
<tr>
<td align=center><font face="arial, helvetica" size="-1"><b>Ticker</b></td>
<td align=center><font face="arial, helvetica" size="-1" color="0000FF"><b>Buy Price</b></td>
<td align=center><font face="arial, helvetica" size="-1"><b>% Profit Targ</b></td>
<td align=center><font face="arial, helvetica" size="-1"><b>Use PT?</b></td>
<td align=center><font face="arial, helvetica" size="-1" color="FF0000"><b>Sell Price</b></td>
</tr>
<tr>
<td align=center><input type=text name=ticker value="NSM" size=8></td>
<td align=center><input type=text name=buyprice value=12.50 size=8></td>
<td align=center><input type=text name=profittarget value=50 size=8></td>
<td align=center><input type=radio name=profityn value="1" size=8 checked>yes
<br><input type=radio name=profityn value="-1" size=8>no</td>
<td align=center><input type=text name=sellprice size=8></td>
<td align=center><input type=button value="Calc!" name=solve size=8 onClick="result()"></td>
</tr>
</table>
</form>

<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tr>
<td align="left"><A HREF="http://?Symbol=NSM"><b>Profile</b></A></td>
</tr>
</table>

<IMG ID='PTform02ImageA' SRC=''>
<IMG ID='Ptform02ImageB' SRC=''>
<BR>
<BR>

</form>

</Body>
</HTML>[/CODE]
Copy linkTweet thisAlerts:
@Jonny_LangAug 15.2005 — katydid:


in the Head:

[CODE]function getProfile(isTicker){


window.location.href = "http://moneycentral.msn.com/investor/research/profile.asp?Symbol="+isTicker+""

}[/CODE]



in the Body:

[CODE]<td align="left" width="2%"><A HREF=javascript:getProfile(document.PTform01.ticker.value)><b>Profile</b></A></td>[/CODE]
Copy linkTweet thisAlerts:
@Jonny_LangAug 15.2005 — P.S.

I wouldn't use a comma as your second split character. I'd use the vertical bar: |
Copy linkTweet thisAlerts:
@katydidauthorAug 16.2005 — J Lang,

Thanks again for your help and insights.

Best of luck in future endeavours!
Copy linkTweet thisAlerts:
@Jonny_LangAug 16.2005 — katydid:

You're welcome. Good luck to you, as well.
×

Success!

Help @katydid spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...