Displaying upper ASCII (code 128-255) in alert() window.

H

hugo2

Most of these characters are not on the standard keyboard.
Here's a successful contrivance, with comments & cautions,
a little page that works in IE6.

<HTML><HEAD><SCRIPT TYPE="text/javascript">
function alt() {
document.all.s1.innerHTML="Current Temp: 68°F";
var txt=document.all.s1.innerText;
alert(txt);
}
</SCRIPT></HEAD>
<BODY>
<INPUT TYPE="button" VALUE="Temperature" onClick="alt()">
<P ID="s1" STYLE="visibility:hidden"> </P>
</BODY></HTML>

comments: The innerHTML property is needed to produce the
character glyph from the entity code. If the entity string
were passed to innerText(in 1st statement) then the code
would remain literal.
This work-around depends on s1 being rendered before alt()
is called. It will not work as immediately executed code,
because element s1 would not exist yet.

cautions: Trying to style alert's display will produce error
msgs. Do not use <B>, <U>, or <I> tags in the argument
string. No Heading tags either.
Strange enough, an inline STYLE, setting font values, say,
does not give error msg, but will not execute either.
Alert ignores it.
You can use <BR> tags in the argument, which give the same
result as \n in a direct arg to alert().

In sum, you can tell alert what characters to display,
in what order, and on what line, but you cannot tell
alert HOW to display them.

hugo2, March 4, 2005
 
R

Randy Webb

hugo2 said:
Most of these characters are not on the standard keyboard.
Here's a successful contrivance, with comments & cautions,
a little page that works in IE6.

Define "works". It does not "work" for me in IE6 on WinXP SP2.

Before posting code, you should at minimum validate the HTML and test it.
<HTML><HEAD><SCRIPT TYPE="text/javascript">
function alt() {
document.all.s1.innerHTML="Current Temp: 68°F";

IE only.
var txt=document.all.s1.innerText;

IE only.
alert(txt);
}

function oneThatWorks(){
document.getElementById('s1').innerHTML="Current Temp: 68°F";
var txt= document.getElementById('s1').innerHTML;
alert(txt);
}

Now, it is tested to work in IE6, Opera 7 and Mozilla.
</SCRIPT></HEAD>
<BODY>
<INPUT TYPE="button" VALUE="Temperature" onClick="alt()">

Object does not support this method.

As reported by IE6 when given a strict doctype.
<P ID="s1" STYLE="visibility:hidden"> </P>
</BODY></HTML>

comments: The innerHTML property is needed to produce the
character glyph from the entity code. If the entity string
were passed to innerText(in 1st statement) then the code
would remain literal.
This work-around depends on s1 being rendered before alt()
is called. It will not work as immediately executed code,
because element s1 would not exist yet.

See above. Reading this groups FAQ will show you how to insert, and
read, text from a div tag in most major browsers without resorting to IE
only code, even though it uses the semi-proprietary (non-standard) innerHTML
cautions: Trying to style alert's display will produce error
msgs. Do not use <B>, <U>, or <I> tags in the argument
string. No Heading tags either.

Who told you that garbage?

Strange enough, an inline STYLE, setting font values, say,
does not give error msg, but will not execute either.
Alert ignores it.

Thats because you can't "style" an alert. If you want style, write your
own alert div tag in the page.
You can use <BR> tags in the argument, which give the same
result as \n in a direct arg to alert().

BULL. Test before you post.

alert('This has a <BR> tag but no line break in the alert, test it!')

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Answer:It destroys the order of the conversation
Question: Why?
Answer: Top-Posting.
Question: Whats the most annoying thing on Usenet?
 
B

bumbleguppy

The easiest way to display characters not on the keyboard is simply:

alert('Current Temp: 68' + String.fromCharCode(176));
 
H

hugo2

Randy said:
Define "works". It does not "work" for me in IE6 on WinXP SP2.

Before posting code, you should at minimum validate the HTML and test it.

IE only.


IE only.


function oneThatWorks(){
document.getElementById('s1').innerHTML="Current Temp: 68°F";
var txt= document.getElementById('s1').innerHTML;
alert(txt);
}

Now, it is tested to work in IE6, Opera 7 and Mozilla.


Object does not support this method.

As reported by IE6 when given a strict doctype.


See above. Reading this groups FAQ will show you how to insert, and
read, text from a div tag in most major browsers without resorting to IE
only code, even though it uses the semi-proprietary (non-standard) innerHTML

Who told you that garbage?



Thats because you can't "style" an alert. If you want style, write your
own alert div tag in the page.


BULL. Test before you post.

alert('This has a <BR> tag but no line break in the alert, test it!')

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Answer:It destroys the order of the conversation
Question: Why?
Answer: Top-Posting.
Question: Whats the most annoying thing on Usenet?

hugo2 March 7, 2004
My apologies to everyone. The one problem with my post
is the function name: alt(). This one name does not work
(I had use several different names) because *alt* turns
out to be an HTML attribute, a rather obscure one in <IMG>,
but I should have checked!
All the other statements I made were derived from testing,
and they are all true, in the context of passing an HTML
fragment containing non-keyboard glyph to alert().
I was not talking about general use of alert().
If interested parties would change the function name to
at() or atl() or go(), then the page works fine,
though you would have to adapt it for other browsers.

hugo2 --
 
R

Randy Webb

hugo2 said:
Randy Webb wrote:

If interested parties would change the function name to
at() or atl() or go(), then the page works fine,
though you would have to adapt it for other browsers.

No need, I gave one that works (and tested) in IE6, Opera 7 and Mozilla,
so its just a matter of copy/paste. Although mine could stand a little
more feature detection before relying on getElementById and innerHTML
(although there has been no definitive test to ensure that changing an
elements innerHTML property actually changes it's visual appearance).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top