View Full Version : How to assign a variable to a string with a "=" and a "?"
dreamingdigital
02-10-2005, 03:44 PM
Sounds easy huh? :eek:
var my_checkout_link = "top.location.href=merchant.mvc?Screen=OINF";
var my_checkout_button = '<p><form name=checkout><input type=button value=" Proceed to Checkout " onClick="'+ my_checkout_link +'"></form></p>';
document.write(my_checkout_button);
When I click on the button it wrote I get this error:
"missing : in conditional expression"
How to assign a variable to a string with a "=" and a "?" that looks like a short if/else statement!! What's correct they way to do this?
Colin
delinear
02-10-2005, 04:08 PM
Try setting the var my_checkout_link using single quotes ' instead of double quotes " - well, it works here so I'm assuming the double quotes causes the text inside to be parsed differently? Worth a try.
jmccaskill
02-10-2005, 04:28 PM
You are setting the value of top.location.href. That value needs to be in quotes:
var my_checkout_link = "top.location.href='merchant.mvc?Screen=OINF'";
Otherwise, it attempts to evaluate a ternary condition expression that is missing the : portion, hence the "missing : in conditional expression" error.
dreamingdigital
02-10-2005, 04:29 PM
Thanks but that still didn't work. There's got to be a real way not just a quick fix. :rolleyes:
CP
Willy Duitt
02-10-2005, 04:44 PM
There is no quick fix!! The real fix is to completely rewrite it to use the DOM and create elements and append them to your document... What you have there will never work.... If you look at the DOM fragment once the document is rendered you will notice that certain attributes such as name, type, or events such as onclick, onmouseover, ect... are not rendered with enclosing quotes even if appearing in the source code... The value is, but only if there are spaces...
Below is a copy of the generated DOM fragment of your code...
<HTML><HEAD>
<SCRIPT>
var my_checkout_link = "top.location.href=merchant.mvc?Screen=OINF";
var my_checkout_button = '<p><form name=checkout><input type=button value=" Proceed to Checkout " onClick="'+ my_checkout_link +'"></form></p>';
document.write(my_checkout_button);
</SCRIPT>
// DOM FRAGMENT -- WHAT IS REALLY WRITTEN TO THE PAGE \\;
<FORM name=checkout></HEAD>
<BODY>
<P><INPUT onclick=top.location.href=merchant.mvc?Screen=OINF type=button value=" Proceed to Checkout "></FORM></P></BODY></HTML>
Another problem is your use of top.location.href which is a javascript method which you are trying to use in a string... Bottomline... you took a shortcut trying to use document write to start with and there is no quick fix...
.....Willy
Basscyst
02-10-2005, 04:44 PM
Your syntax is a little off that's all, and you probaly want to put the action into a function, see how the below is working?
<script type="text/javascript">
function changeSrc(url)
{
top.location.href=url;
}
var my_checkout_button = '<p><form name="checkout"><input type="button" value="Proceed to Checkout" onClick="changeSrc(\'merchant.mvc?Screen=OINF\')"></form></p>';
document.write(my_checkout_button);
</script>
Willy is right though, this really isn't the best way to go about this. What happens to people with javascript disabled? They just get nothing.
Basscyst
dreamingdigital
02-10-2005, 05:46 PM
Thanks everyone. It's good now. :)
Here's the big picture: It's JavaScript combined with Miva OpenUI tokens. I wanted to have a "You have $XX.XX remaining" message but JavaScript is too limited (or I can't seem to get the decimals right is more like it) Is anybody insterested in seeing what I tried for that? Otherwise I'll stick with this:
<script language="JavaScript" type="text/javascript">
function changeSrc(my_url) {
top.location.href = my_url;
}
var my_checkout_button = '<p><form name="checkout"><input type="button" value="Proceed to Checkout" onClick="changeSrc(\'%VAR(g.secure_sessionurl)%Screen=%IF(login)%OINF%ELSE%LOGN&Order=1%IFEND%\')"></form></p>';
var my_minimum_purchase_amount = 100;
var my_basket_total = "%basket_total%";
if (my_basket_total >= my_minimum_purchase_amount) {
document.write(my_checkout_button);
}
</script>
Thanks again :thumbsup:
CP
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.