Solved

Javascript: SPLIT input string but retain deg symbol

Posted on 2006-11-20
14
736 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
ID: 17985560
alert(unescape(strLatLon));
0
 
LVL 3

Expert Comment

by:Masoudgh
ID: 17986506
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
ID: 17986538
for more information check here : http://www.w3schools.com/jsref/jsref_unescape.asp
0
 

Author Comment

by:markp99
ID: 17986613
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
ID: 17986659
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
ID: 17986688
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 7

Expert Comment

by:mark-b
ID: 17989539
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
ID: 17990445
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
ID: 17990806
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
ID: 17991004
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
ID: 17991017
I am using IE 6 & 7, but would also like to support Firefox.
0
 

Author Comment

by:markp99
ID: 17991375
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
ID: 18189325
PAQed with points refunded (500)

Computer101
EE Admin
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SharePoint 2013 Blog Template 2 37
Auto refresh part of an MVC5 View 7 61
Angular - "nest" service calls? 18 25
Loop and hide/show dropdown option with jQuery 4 22
I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…

895 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

13 Experts available now in Live!

Get 1:1 Help Now