Link to home
Start Free TrialLog in
Avatar of markp99
markp99

asked on

Javascript: SPLIT input string but retain deg symbol

I'm taking a line of input from an external file and parsing the comma delimited data using the SPLIT command as follows:

               var strLatLon = content.split(",")[10];       // Decimal LatLon

The command retrieves the expected data from the input string, but I am losing the degrees symbol from the data.  The following is an example of the LatLon I am trying to capture:

               "N 42° 45.103;W 71° 29.167"    (data is quoted in the input file, I do want to collect the LatLon as a pair as a single variable)

When I display the value:  alert (strLatLon), I see a BLOCK in the position of the degrees symbol.

               "N 42� 43.743;W 71� 26.850"

Can someone please suggest an approach which will preserve (or restore) the degrees symbol in this string?



Thanks!

Avatar of Alkali_Guy
Alkali_Guy

alert(unescape(strLatLon));
hi,
you can use unescape function which decodes an encoded string like this (strange characters you indicate)
this type of statements can create by another function like escape.

so your code must change like this:
alert unescape((strLatLon));
Avatar of markp99

ASKER

Thanks for the suggestion.

Neither form presents the degrees symbol:

     unescape(strLatLon);
     unescape((strLatLon));

At home I see a "block" character,  here at work I see a "?" in place of the degrees symbol.

The input file (.txt) shows the degrees symbol properly.



Any additional thoughts?
yeah I don't check "�", this code convert to a block which you said. I think it's better to copy the complete code you used.
I found something new, I think you use a browser like FIREFOX at your work and use something like IE at your home. "�" in firefox show as a diamond with question mark and in IE show as a block. But I think the complete code may help us to solve the problem.
Did you try setting the character set encoding of your html page to be the same as the text file you are loading from?

For example, set the charset on the page to use iso-8859-1:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Strict//EN">
<html>
      <head>
       <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
       <script language="JavaScript1.2">
         var latLong = "latLong "; // load the latLong in this variable from the file
        
         alert( latLong );
       </script>  
      </head>
      <body></body>
</html>

Don't know if this will work, since I can't repeat the problem.  But it may be worth a shot.
Avatar of markp99

ASKER

mark-b,

I was hopeful your sugestions would have solved my problem, but I get the same bad result.

Your snippet of code is basically what I am attempting, except with a reference to an external file which I step thru line-by-line to grab the comma-delimited string.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Strict//EN">
<html>
     <head>
      <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
      <script language="JavaScript1.2">

       <!read the string from the external file>
       <!SPLIT the string to parse data elements>

        var latLong = "latLong "; // load the latLong in this variable from the file
        alert( latLong );

     </script>  
     </head>
     <body></body>
</html>

The following is a typical line from that external file:

String = -71.45935,42.762917,GCWJFA,"Riverfront Promenade","String Theory",T,2.0,1.0,Micro,N,N 42° 45.775;W 71° 27.561

The problem data element is at the END of thae string at comma position #10 (the deg symbol was present when I posted this msg, but will be probably be escaped when you read this).


Thanks for your help!
Hmm.  Well, I'm not sure how you are loading the data, but this works in Firefox:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Strict//EN">
<html>
     <head>
      <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
      <script language="JavaScript1.2">
         var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
         if ( window.XMLHttpRequest ) xhr.overrideMimeType("text/plain; charset=ISO-8859-1");
         xhr.open("GET", "data.txt", true);
         xhr.onreadystatechange=function() {
           if (xhr.readyState==4) {
             var latLong=xhr.responseText;
             alert( latLong.split(",")[10] );
           }
        }
        xhr.send(null);
      <script>  
     </head>
     <body></body>
</html>

My data file for this example is a file called 'data.txt' and it only has that one line in your example.  The problem with my example is does not work in IE because the XMLHttpRequest ActiveX object in IE does not support he overrideMimeType method.  IE 7 might, but IE 6 does not.

How are you loading the text file?

-Mark
Avatar of markp99

ASKER

Mark,

Here is basic idea:

var page=[];
page["geoFile"] = "GSAK.txt";

parseFile = function(geoString) {
      infoW = geoString.split("\n");
      for (var i=0; i<infoW.length; i++) {
            var content = infoW[i];
            if (content.indexOf(",") != -1) {
                  var lat = content.split(",")[1]*1;              
                  var lng = content.split(",")[0]*1;        
                  ....collect more variables...
                  var strLatLon = content.split(",")[10];  
            ...do some more stuff...
            alert(strLatLon);
            }
      }
}

function mungeFile(iso){
      GDownloadUrl(page[iso],parseFile);
}

mungeFile('geoFile');  //This call used in a number of locations in the js file



BTW, this is a Google Maps app.


Thanks for your assistance!
Avatar of markp99

ASKER

I am using IE 6 & 7, but would also like to support Firefox.
Avatar of markp99

ASKER

I found a bit of a brute force solution:

I replaced "°" with a valid ascii character "^" when I export from my database to the .txt input file. When I get the data into .js, I simply replace "^" back to "°" and all is well.

A nice simple solution to a bugger of a problem.


I'll call this my solution and will move on.  :)


Thanks for the effort, Masoudgh & mark-b
ASKER CERTIFIED SOLUTION
Avatar of Computer101
Computer101
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial