Solved

Javascript: SPLIT input string but retain deg symbol

Posted on 2006-11-20
14
734 Views
Last Modified: 2010-08-05
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!

0
Comment
Question by:markp99
  • 5
  • 4
  • 2
  • +2
14 Comments
 
LVL 5

Expert Comment

by:Alkali_Guy
Comment Utility
alert(unescape(strLatLon));
0
 
LVL 3

Expert Comment

by:Masoudgh
Comment Utility
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));
0
 
LVL 3

Expert Comment

by:Masoudgh
Comment Utility
for more information check here : http://www.w3schools.com/jsref/jsref_unescape.asp
0
 

Author Comment

by:markp99
Comment Utility
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?
0
 
LVL 3

Expert Comment

by:Masoudgh
Comment Utility
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.
0
 
LVL 3

Expert Comment

by:Masoudgh
Comment Utility
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.
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 7

Expert Comment

by:mark-b
Comment Utility
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.
0
 

Author Comment

by:markp99
Comment Utility
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!
0
 
LVL 7

Expert Comment

by:mark-b
Comment Utility
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
0
 

Author Comment

by:markp99
Comment Utility
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!
0
 

Author Comment

by:markp99
Comment Utility
I am using IE 6 & 7, but would also like to support Firefox.
0
 

Author Comment

by:markp99
Comment Utility
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
0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
Comment Utility
PAQed with points refunded (500)

Computer101
EE Admin
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

762 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now