Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
|
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 18th, 2004, 09:40 AM
Authorized User
 
Join Date: May 2004
Posts: 28
Thanks: 0
Thanked 1 Time in 1 Post
Default Javascript to HTML Special Characters

We have a page which has some dynamic combo boxes which are initially populated data from a database which contain HTML special characters such as uml and acutes. However as they are dynamic various parts of the page populate the combo box with a refreshed list via a Javascript Array. However this then shows the special characters literally i.e. é rather than the actual character.

how can we get round this. here is a small sample page to show our problem.

<html>

    <head>
        <script>
            test = new Array;
            test[0]=new Array("T&auml;bert","test");

            test2 = "T&auml;ubert";

            function setVal1()
            {
                document.gui.aSelect.options[0].text = "1 - " + test[0][0];
            }

            function setVal2()
            {
                document.gui.aSelect.options[0].text = "2 - T&auml;ubert";
                escapeXml="false"
            }
        </script>
    </head>

    <body>

        <FORM NAME="gui">
            <SELECT NAME="aSelect">
                <OPTION VALUE="deafert">Taubert
            </SELECT>
            <P>
            <INPUT TYPE="button" VALUE="Copy from array in JS" onclick="setVal1()">
            <P>
            <INPUT TYPE="button" VALUE="Set explicitly from JS function" onclick="setVal2()">
            <P>
            <INPUT TYPE="button" VALUE="Set explicitly from HTML" onclick="document.gui.aSelect.options[0].text = '3 - T&auml;ubert'">
            <P>
            <INPUT TYPE="button" VALUE="Copy from JS array explicitly from HTML" onclick="document.gui.aSelect.options[0].text = '4 - ' + test[0][0]">
            <P>
            <INPUT TYPE="button" VALUE="Copy from JS variable explicitly from HTML" onclick="document.gui.aSelect.options[0].text = '5 - ' + test2">
        </FORM>
    </body>

</html>

 
Old May 18th, 2004, 11:38 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
Send a message via Yahoo to melvik
Default

Sorry but I couldnt get ur question! its fine in my opinian since it show what evr u set for?! what do u want to be displayed?!

Kepp in touch.

Always:),
Hovik Melkomian.
 
Old May 19th, 2004, 02:49 AM
Authorized User
 
Join Date: May 2004
Posts: 28
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hovik

Thanks for the reply.

What I want is to display the a uml character. If you run the code I provided press the 3rd button "Set Explicitly from HTML" then look in the box thats what I want, an a with 2 dots over it which is an uml used in european languages.

In HTML this is done by using &auml; but in javascript its /344. The problem is that when the text is passed out its not converting it into the HTML equivalent.

You will notice in the code that I'm using the same string in all examples of the button but only the 3rd will display the correct format. I need a way to do this for all of the buttons in this example or use so sort of Javascript function to return the pure html code.

Cheers

Andy Gilfrin...



 
Old May 19th, 2004, 03:28 AM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 683
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi Andy,

The reason the third method works is that entities in the onclick attribute of the button are parsed when the page is rendered.

One (dirty) way to do what you want would be to create a hidden element in your page e.g.

<div id="Parser" style="display:none;"></div>

and populate it using innerHTML, causing the entities to be parsed then copy the innerHTML into the text property of the option element...

function SetVal(){
    var d = document;
    var p = d.getElementById("Parser");
    p.innerHTML = "2 - T&auml;ubert";
    d.gui.aSelect.options[0].text = p.innerHTML;
}

HTH,

Chris

 
Old May 19th, 2004, 04:33 AM
Authorized User
 
Join Date: May 2004
Posts: 28
Thanks: 0
Thanked 1 Time in 1 Post
Default

Thanks Chris.

Thank works a treat, but I'm not sure if thats going to be suitable as we are currently using a Javascript array to populate the values.

The main problem is that its not just one page way might have this problem on it could be lots of pages. But thanks for the info and it gives me a starting point to go back to the team to see what we can do.

Maybe we could build a function to get the data from the array and then return that using the method you described. I'll have to look at the code to see how it gets the data from the array.

Thanks alot.

Andy Gilfrin

:)

 
Old May 19th, 2004, 05:01 AM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 683
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi Andy,

One way to tackle it might be to create a form in your page (server-side from the database) that contains hidden fields:

<form id="ArraySrc">
    <input type="hidden" value="1 - &quot;value&quot;" />
    <input type="hidden" value="T&auml;ubert" />

</form>

Then use it to populate your array when the page loads (call LoadArray() from the body onload)...

var gArray;
function LoadArray(){
    gArray = new Array();
    var src = document.forms["ArraySrc"];
    for(var i = 0; i < src.elements.length; i++){
        gArray[i] = src.elements[i].value;
    }
}

Then just access the array as normal.

Best regards,

Chris



 
Old May 19th, 2004, 09:25 AM
Authorized User
 
Join Date: May 2004
Posts: 28
Thanks: 0
Thanked 1 Time in 1 Post
Default

Thanks for the follow up Chris.

I actually managed to get it working simply using the method you described. I created a function as showed then called that passing in the array reference and then passing out the HTML values.

I works fine so thanks once again.

Andy Gilfrin...






Similar Threads
Thread Thread Starter Forum Replies Last Post
New help with Pattern for special characters 2BOrNot2B Java Basics 1 July 2nd, 2008 04:17 PM
Inserting Special Characters From JavaScript richard.york Javascript 5 June 9th, 2005 01:04 PM
storing special special characters in nvarchar... ACE2084 SQL Server 2000 2 February 9th, 2005 11:45 AM
special characters lian_a Classic ASP Basics 3 June 23rd, 2004 05:16 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.