Forum Moderators: open

Message Too Old, No Replies

How display HTML from database in Javascript alertbox?

         

Lingu

3:20 pm on Feb 24, 2009 (gmt 0)

10+ Year Member



Hello,

I use a javascript alertbox to display user feedback on a language learning site.

The strings are retrieved from a MySQL database. Some special characters are stored as html in the database:
wǒ tàitai

These strings are displayed correctly on the web page (the ǒ is an o with a diacritic curl above), but when shown in the alertbox the string is shown as literally "wǒ tàitai"

Is there any way to display the special characters correctly, i.e. to make the alertbox interpret the string in the same way as the web page?

The alertbox code currently used is this one:
+++++++++++++++++
<script type="application/x-javascript" language="JavaScript">

<!--

arComMist = new Array()
<%
for i=0 to UBound(arJscript)
if arJscript(i,0) > 0 then
Response.write "arComMist["&arJscript(i,0)&"] = new Array(2);" & VBCrLf
Response.write "arComMist["&arJscript(i,0)&"][0] = '"&arJscript(i,1)&"';" & VBCrLf
Response.write "arComMist["&arJscript(i,0)&"][1] = '"&arJscript(i,2)&"';" & VBCrLf
End if

Next
%>
function alertBox(iComMistId) {
alert(arComMist[iComMistId][0]+"\n\nRätt svar är: "+arComMist[iComMistId][1]);
}

// -->
</script>
++++++++++++++++++++++++++++++++

Greatful for any heelp on this,

Jakob H.

[edited by: eelixduppy at 3:35 am (utc) on Feb. 25, 2009]

Rambo Tribble

10:16 pm on Feb 26, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Although it may exist, I've never seen a method to convert a character entity to a displayed character, other than the HTML rendering engine in the browser. Since an alert is designed to display simple text, it appears the full rendering engine is not applied to it.

The simplest approach might be to create a floating div which simulates an alert, since then the full rendering capabilities would be applied to the div content.

Lingu

5:52 am on Feb 27, 2009 (gmt 0)

10+ Year Member



Thanks for your reply, but is there no way to make the javascript interpret the string as html? When the page is displayed the source looks as follows:

<script type="application/x-javascript" language="JavaScript">

<!--

arComMist = new Array()
arComMist[8] = new Array(2);
arComMist[8][0] = 'det är fel!';
arComMist[8][1] = 'w&#466; tàitai';

function alertBox(iComMistId) {
alert(arComMist[iComMistId][0]+"\n\nRätt svar är: "+arComMist[iComMistId][1]);
}

// -->
</script>

(It´s the "'w&#466; tàitai';" that needs to be interpreted as html. Could this not be handled with quotes etc.?)

What is a floating div? How would that code look?

Many thanks again,

Jakob H.

Rambo Tribble

2:10 am on Feb 28, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



JavaScript deals in objects which may be HTML tags, properties and such, but does not interpret them in the context of HTML. In other words, the problem isn't that JavaScript that doesn't understand the entity, the problem stems from the browser not accepting the HTML entity in the alert box context. Short of reworking the browser's internals, I don't see any way to accomplish it.

When I say "floating div", (not to be confused with a div that has a CSS positioning property of "float"), I simply mean a div absolutely positioned over the page. The following illustrates creating the div on the fly. Perhaps an easier approach is to create the div in the HTML markup and assign it display:none. You can then make it visible when you need it. Obviously, more styling would be necessary to simulate an alert box.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title></title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<style type="text/css">
</style>
<script type="text/javascript">
function floatDiv(){
var new_div=document.createElement("div");
new_div.style.position="absolute";
new_div.style.top="200px";
new_div.style.left="200px";
new_div.style.height="300px";
new_div.style.width="400px";
new_div.style.backgroundColor="#ddbbaa";
new_div.id="alert_div";
document.getElementById("pg_bod").appendChild(new_div);
var new_txt=document.createTextNode("your alert message");
document.getElementById("alert_div").appendChild(new_txt);
}
</script>
</head>
<body id="pg_bod">
<p>
<a href="#" onclick="floatDiv();">test</a>
</p>
</body>
</html>