Solved

How do i get javscript to interpret my entity names corrctly

Posted on 2011-03-13
7
216 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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 143

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Learn by example how to specify CSS selectors for Selenium WebDriver test automation software.
Any business that wants to seriously grow needs to keep the needs and desires of an international audience of their websites in mind. Making a website friendly to international users isn’t prohibitively expensive and can provide an incredible return…
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
The viewer will learn how to count occurrences of each item in an array.

829 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