Solved

How do i get javscript to interpret my entity names corrctly

Posted on 2011-03-13
7
213 Views
Last Modified: 2012-05-11
I have a variable in one of my scripts which adds a currency sign to the beginning of whatever amount is calculated. The variable looks like this when using pounds sterling:

var currency = "£";

The problem is that using a regular £ sign does not work and a small square appears in its place. I have tried using an entity name like this:

var currency = "£"; but instead of a pound sign the script reproduces the entity name verbatim. This is also the case if I use an entity number.

Is there a way of using an entity name but having it reproduced on the page as the symbol it stands for?
0
Comment
Question by:grigorovsky
7 Comments
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 35122733
Depending on the codepage of your site the currency symbol should work. Please show the code you have now since entities ALSO should work
0
 

Author Comment

by:grigorovsky
ID: 35122886
Not sure which code you're referring to. The document type statement is:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

Open in new window


The javascript code (with currency statement in bold) is:

<!-- Begin
var max_units = 10; // quantities in excess of max_units all have the same unit price
[b]var currency = "&pound;"; // currency sign used in 'formatMessage()'//[/b]
Edit this function to reflect your discount prices! 
function getDiscountPrice(units) {
// Note: It is important to work your way down from max to min amounts!
if (units >= max_units) return 0.00;
if (units == 9) return 0.00;
if (units == 8) return 0.00;
if (units == 7) return 0.00;
if (units == 6) return 0.00;
if (units == 5) return 28.00;
if (units == 4) return 30.00;
if (units == 3) return 32.00;
if (units == 2) return 32.50;
if (units == 1) return 35.00;
if (units <= 0) return 0.00;
}
function getNumberOfUnits() {
var units = document.ents_Signup.units.value; 
return (units == "") ? 0 : units;
}
function showResult(result) {
// adjust the following line if result must popup somewhere else
document.ents_Signup.respons.value = result;
document.ents_Signup.amount.value = result2;
}
function formatMessage(units, unit_price) {
return " @ " + currency + formatPrice(unit_price);
}
function formatMessagee(units, unit_price) {
return formatPrice(units * unit_price);
}
// AltUnits (alternate units): add extra units to reach minimum for next discount price
function getAltUnits(units) {
var discount_price = getDiscountPrice(units);
if (units > max_units) do { units++ } while (discount_price == getDiscountPrice(units));
return units;
}
function findPrice() {
var units = getNumberOfUnits();
var unit_price = getDiscountPrice(units);
var alt_units = getAltUnits(units);
var alt_unit_price = getDiscountPrice(alt_units);
var result;
if ((units * unit_price) < (alt_units * alt_unit_price)) 
result = formatMessage(units, unit_price);
else
result = formatMessage(alt_units, alt_unit_price);
if ((units * unit_price) < (alt_units * alt_unit_price)) 
result2 = formatMessagee(units, unit_price); 
else
result2 = formatMessagee(alt_units, alt_unit_price);
showResult(result);
}
function formatPrice(value) {
var result= Math.floor(value) + ".";
var cents = 100 * (value-Math.floor(value)) + 0.5;
result += Math.floor(cents / 10);
result += Math.floor(cents % 10);
return result;
}
function filterNonNumeric(field) {
var result = new String();
var numbers = "012345";
var chars = field.value.split(""); // create array 
for (i = 0; i < chars.length; i++) {
if (numbers.indexOf(chars[i]) != -1) result += chars[i];
}
if (field.value != result) field.value = result;
}
//  End -->

Open in new window

0
 
LVL 9

Expert Comment

by:Roman Gherman
ID: 35123127
Hi,
This can also happen because that you file were you put pound sign is saved using an wrong encoding.
Try to save the file using a different encoding, using notepad or a similar application.

I also had an issue like that in the past, and when I changed the encoding everything went OK.
Unfortunately I can't remember what encoding I have used.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:grigorovsky
ID: 35123170
The thing is that I use the entity name "&pound;" elsewhere in the same document and it renders perfectly. It is just the javascript rendering that is not working!
0
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 500 total points
ID: 35123518
try

var currency = "\u00A3";

and I think when you use the pound sign, you had saved you document in another codepage than utf8 - then it does not matter you have the meta tag. the file must be saved as utf8 to be show as such too
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 36123569
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now