Solved

Apostophe (') causes an error. Why?

Posted on 2004-10-26
200 Views
Last Modified: 2008-01-09
Hello again Experts

I have a problem that I can't figure out for the life of me.

I have put the code here for you to play around. The problem is, if I use an apostrophe within the data array I get an error. It happens even if I use an escape (\'), decimal (') or as itself (') and I can't figure out why.

Here's the code on the first page.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>

<title>Property Details</title>

<script language="JavaScript">
// Create a Table from a JavaScript Array script by lil_Puffball at http://www.experts-exchange.com //
// loadDetails() script by ren_b at http://www.experts-exchange.com  modified for use here by me.//
// Put together by lil_Puffball and me//

var data=new Array();

data[0] = " | 914 NW 82nd Street, Bradenton | $298,000 | 4 | 2 | 1,960 | This is a 4 bedroom 2 bath pine meadow home overlooking a beautiful lake. All the sliding doors are pocket doors that open into a beautiful pool area that has just been re screened. Many updates including: jacuzzi tub, laminated wood floors, vaulted ceilings, attic fan, new trane heat pump, newer water heater, second bath opens into pool area, underground sprinkler system feed from lake (new pump). | "

data[1] = " | 8843 Etera Drive, Sarasota | $227,000 | 3 | 2 | 1,671 | Incredible floor plan, with two major upgrades 1) Extended family room / den adding an extra 140sqft 2) Living room wall unit, that looks beautiful with the textured vaulted ceiling. The Village Walk community has it all… Town Center, entire lawn maintenance, 2 heated pools, tennis courts, security, fitness center, shops, salon, bank, post office, café and deli, wide paths, lakes, bridges. Everything you need for the ultimate Florida lifestyle. | "

data[2] = " | 1125 North Bay East Street, Tampa | $111,900 | 4 | 2 | 1,576 | Needing A little TLC this 4/2 1576 sqft home makes the perfect investment. Subject has appraised in the 130s so consider having your closing costs included into the price. Big laundry area and large living room. Air con is only 3 yrs old. Large shed outside. Bring your buyers and bring your investor. | "

for(i=0;i<data.length;i++){data[i]=data[i].split("| ");}

for(i=0;i<data.length;i++){data[i][0]=" ";}

for(i=0;i<data.length;i++){data[i][1]="<b>"+data[i][1]+"</b>";}

for(i=0;i<data.length;i++){data[i][2]="Price: "+data[i][2]+"";}
for(i=0;i<data.length;i++){data[i][3]="Bedrooms: "+data[i][3]+"";}
for(i=0;i<data.length;i++){data[i][4]="Bathrooms: "+data[i][4]+"";}
for(i=0;i<data.length;i++){data[i][5]="Total Area: "+data[i][5]+" square feet";}
//for(i=0;i<data.length;i++){data[i][6]=" ";}
for(i=0;i<data.length;i++){data[i][7]="<a href='#' onclick=\"loadDetails('"+data[i][1]+"','"+data[i][6]+"')\" style='color:red'>... more</a><hr>";}

function loadDetails(HoName, HoDetails) {
  document.location.href = "viewer.htm?"+escape(HoName)+"&"+escape(HoDetails);
}

function createTable(){
  var numcols=1;
  var numrows=data[0].length,trow,tcell;
  var tbl=document.getElementById("container").firstChild;
  for(n=0;n<data.length;n+=numcols){
    for(i=0;i<numrows;i++){
      trow=document.createElement("tr");
      for(j=0;j<numcols;j++){
        tcell=document.createElement("td");
        if(!data[n+j]){tcell.innerHTML="";}
        else{tcell.innerHTML=data[n+j][i];}
        trow.appendChild(tcell);
      }
      tbl.appendChild(trow);
    }
  }
}


</script>

</head>

<body onload="createTable()">

<table id="container" align="center" cellspacing="0" cellpadding="0" border="0" width="400"><tbody></tbody></table>

</body>
</html>

And here is the code for the second page called 'viewer.htm'

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Details Viewer</title>

<script language="JavaScript" type="text/javascript">
// read_pants() Script by ren_b at http://www.experts-exchange.com. Used with loadDetails() script.  //
// All the other nonsense by me //

var pants = new Array();
function read_pants() {
      pants = document.location.toString().split('?')[1].split('&');
      for(i in pants)
      pants[i] = unescape(pants[i]);

      document.write(pants[0]);
      document.write("<p>");
      document.write(pants[1]);
}

</script>

</head>

<body>

<script>read_pants();</script>

</body>
</html>

I know all the coding works as I have used it successfully before, however, I didn't need to use an apostrophe.

Hope you can help.

Regards

Pantyboy
0
Question by:Pantyboy
    6 Comments
     
    LVL 63

    Expert Comment

    by:Zvonko
    Change your function read_pants()  to this:

    var pants = new Array();
    function read_pants() {
         if(document.location.search){
           pants = document.location.search.split('?')[1].split('&');
           for(i in pants)
             pants[i] = unescape(pants[i]);

         }
           document.write(pants[0]);
           document.write("<p>");
           document.write(pants[1]);
    }

    0
     
    LVL 31

    Expert Comment

    by:GwynforWeb
    a fails afe way of solving the ' problem is this, for:-  Micheal's book

    data="Micheal"+"'"+"s book"
    0
     
    LVL 1

    Author Comment

    by:Pantyboy
    Hi both

    Many thanks for the replies. However, firstly, Zvonko, my apologies, I should have specified that the error appears on the first page. If you leave omit apostophes within the data[#]... array the second page loads without any problems. The error comes on the first page when an apostrophe is used in the text of the data[#] array. for example. In data[2] = "..." the line ...appraised in the 130s... should really read ...appraised in the 130's... The apostrophe between 130 and s creates the error. I hope that helps. Incidently, you can't use escaped quotation marks (\") either.

    Secondly, GwynforWeb, I want to keep the writing of the data as simple as possible, as the person who will be updating it has very little knowledge. My intention is to put the data into an external file once it's all working. Also it doesn't work in this case.

    Sorry both.

    Kind regards

    Pantyboy
    0
     
    LVL 1

    Author Comment

    by:Pantyboy
    Hello again

    Something else I forgot to mention (slapped wrists all round). The apostrophe only affects the data[#] statement it appears in.

    ie: If an apostrophe appears in statement data[2] it will only affect that statement. The statements, data[0] and data[1], will send their data to viewer.htm correctly, provided they do not contain apostrophes.

    I don't know if that narrows down the problem.

    Regards

    Pantyboy
    0
     
    LVL 1

    Author Comment

    by:Pantyboy
    Hello again

    I've found a way round my apostrophe situation. Because I couldn't use ', \', or #39; I was stumped if an apostrophe was required.

    Under 'Additional named entities for HTML' at http://www.w3.org/TR/WD-entities-961125, 'Appendix C: Character Entities for special symbols and BIDI text' shows a character which works.

    It is the 'right single quotation mark' which is called by using '&rsquo;' or '&#8217;'.

    A big thank you goes out to Zvonko and GwynforWeb for trying to resolve the problem. I have now 'aquired' another situation which I need help with and I will post once I get it straight in my head the best way to ask the question.

    Regards

    Pantyboy
    0
     

    Accepted Solution

    by:
    Closed, 250 points refunded.
    PashaMod
    Community Support Moderator
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    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 …
    The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
    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…

    934 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

    18 Experts available now in Live!

    Get 1:1 Help Now