Solved

How do i get javscript to interpret my entity names corrctly

Posted on 2011-03-13
7
218 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

 

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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

Because your company can’t afford for you to make SEO mistakes, you’ll want to ensure you’re taking the right steps each and every time you post a new piece of content. This list of optimization do’s and don’ts can help you become an SEO wizard.
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

695 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