Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
|
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To 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 December 10th, 2007, 01:11 PM
Registered User
 
Join Date: Dec 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Help: Arrays using JavaScript,/JSP Iterator in IE

Thank you for looking at this problem!

I am having major problems with getting IE 6 and 7 trying to access XML generated from a Custom Object Array. If someone can sort this out for me il give them 10 Duke stars. This problem has been driving me crazy...

First il describe my application... Basically it should display items on a map. But before that I need it to access members of the Array in question....

Firstly I have an object called MappingDisplay....

Code:
public class MappingDisplay implements Serializable { 
private static final long serialVersionUID = -4293462259215498921L; 
private String _vehicleName; 
private double _latitude; 
private double _longitude; 
 
public MappingDisplay(String vehicleName, double latitude, double longitude) 
{ 
this._vehicleName = vehicleName; 
this._latitude= latitude; 
this._longitude= longitude; 
} 
 
public String getVehicleName() { 
return _vehicleName; 
} 
 
public double getLongitude() { 
return _longitude; 
} 
 
public double getLatitude() { 
return _latitude; 
}

Then in a Struts Action I create an Array of MappingDisplays and put them in the session...


Code:
// returns a List of MappingDisplay objects 
List mappingList = getMappingDisplayList(form); 
// creates and populates the Array that goes in the session 
 
MappingDisplay[] mappingArray = new MappingDisplay[mappingList.size()]; 
for (int i = 0; i < mappingList.size(); i++) { 
mappingArray[i] = (MappingDisplay) mappingList.get(i); 
} 
session.setAttribute("mappingArray", mappingArray);

The mappingArray definately gets populated properly because the JavaScript im using for FireFox gets the values correctly... I have also debugged the code and it is fine.

I am using Tiles to insert a JSP with an iterator to iterate through the objects in the Array in the session and create XML (stored on the server AFAICT ?)

itemOnMapIterator.jsp...

Code:
<div id="xmlData" style="display:none">
<c:forEach var="mappingDisplay" varStatus="current" items="${mappingArray}">
<c:if test="${current.last}">
<c:set var="numberOfVehicles" value="${current.count}"/>
</c:if>
<mappingDisplay>
<vehicleName><c:out value="${mappingDisplay.vehicleName}"/></vehicleName>
<latitude><c:out value="${mappingDisplay.latitude}"/></latitude>
<longitude><c:out value="${mappingDisplay.longitude}"/></longitude>
</mappingDisplay>
</c:forEach>
</div>

Then on a separate JSP I am trying to access the values of the XML created from the Array.

The code for Firefox works fine...

mapsFormLayout.jsp

Code:
function parseXmlContent()
{                                    
    var browserLabel="";
    // custom browser checker    
    if (BrowserDetect.browser == "Explorer") 
    {
        browserLabel="IE";
        alert("browser= " + browserLabel);
        getMappingData();
    }

    else
    {
        browserLabel="FireFox";
        alert("browser= " + browserLabel);

        var objectPosition = document.getElementById('xmlData').getElementsByTagName("mappingDisplay");
 
        alert("objectPosition - " + objectPosition.length);

         for(var i = 0; i < objectPosition.length ; i++)
               {
               var vehicleName = objectPosition[i].getElementsByTagName("vehicleName").item(0).firstChild.data;       
                    alert("vehicleName " + vehicleName); 
             }
        }
    }
When I load the page in FireFox the alert correctly gives me the name of the vehicle. However when I use the same code in IE I get many errors such as

"Error: Object required" or "Error: Object doesn’t support this property or method"

I have read here, and in other places that IE cannot access the Array notes by using
.item(0).firstChild.data etc

So I have tried to build the JavaScript method getMappingData(); above so that IE can access the elements on the Array... This is as far as I have gotten...


Code:
function getMappingData() 
    {
    // THIS IS PROBABLY WRONG
    var objectPosition = document.getElementById('xmlData').getElementsByTagName("mappingDisplay");

    var maxMaps = objectPosition.length;
    alert("maxMaps: " + maxMaps); // fires correctly

    // CODE NEEDED HERE
    }
When the page is loaded I am checking vehicle checkboxes and submitting the form and that puts the values in the MappingDisplay. I have debugged the Java and used the alerts on the FireFox JS code to read the values and it reads them all fine.

The alert above tells me the correct length of the Array but I cant access the members!
I have a feeling the I am not accessing the array properly in the IE JavaScript. It seems to recognise the length but doesn’t recognise it as an Array or cant seem to access its members.

All I want to do is put the correct vehicleName, longitude and latitude in an alert.

I have used various ways to get the JavaScript when browsing with IE 6/7 to read the values in the Array but to no avail...

I have been stuck at this one for quite a while!
Any help, and suggestions would be deeply appreciated!



 
Old December 10th, 2007, 01:43 PM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Can you do a view-source of the page in IE and post the div xmlData and its contents?

--

Joe (Microsoft MVP - XML)
 
Old December 11th, 2007, 05:15 AM
Registered User
 
Join Date: Dec 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by joefawcett
 Can you do a view-source of the page in IE and post the div xmlData and its contents?

--

Joe (Microsoft MVP - XML)
Thanks for the reply Joe
When I do a view-source of the page in IE I cant find the Div xmlData.
However I have used this code late last night and it prints all the values...

Code:
var objectPosition = document.getElementById('xmlData').getElementsByTagName("mappingDisplay");

alert("objectPosition - " + objectPosition.length);

    for(var i = 0; i < objectPosition.length ; i++)
     {
    alert("innerText= " + document.getElementById("xmlData").innerText);
    alert("innerHTML= " + document.getElementById("xmlData").innerHTML);

     }
And the alerts say this:

"innerText= vehicleName1 51.254663111111 -2.05014456321"

and

"innerHTML= <mappingDisplay><vehicleName>vehicleName1</vehicleName><latitude>51.254663111111</latitude>
<longitude>-2.05014456321</longitude></mappingDisplay>"


So I take it that objectPosition is being read as a HTMLObject and not an Array.
Does anyone have the syntax to declare objectPosition as an Array and populate it so that I could iterate through it and print out the VehicleName, Longitude and Latitude separately?

 
Old December 11th, 2007, 10:55 AM
Registered User
 
Join Date: Dec 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Its Ok,
I sorted it out all by myself.

Here is the code I am using. It basically

Code:
function getXmlElements()
    {
        if (BrowserDetect.browser == "Explorer") 
        {
            //browserLabel="IE";
            alert("browser= " + browserLabel);

            var objectPos = document.getElementById("xmlData");

              var i = 0;
              var maxMaps = objectPosition.length;

            alert("objectPos.innerText=" + objectPos.innerText);

            var tempPos = objectPos.innerText;
            var temp = new Array();
            temp = tempPos.split(' ');

            for(i; i< temp.length -1; i+=3 )
            {
                alert("vehicleName: " + temp[i])
                alert("longitude: " + temp[i+1])
                alert("lattiude: " + temp[i+2])
            }
        }
        else
        {
            browserLabel="FireFox";
            alert("browser= " + browserLabel);

        var vehicleName;
            var latitude;
            var longitude;

             for(var i = 0; i < objectPosition.length ; i++)
               {
                    vehicleName = objectPosition[i].getElementsByTagName("vehicleName")    
                objLatitude = objectPosition[i].getElementsByTagName("latitude")            
                objLongitude = objectPosition[i].getElementsByTagName("longitude")

            alert("vehicleName: " + vehicleName[0].firstChild.data)
            alert("longitude: " + objLongitude[0].firstChild.data)
            alert("lattiude: " + objLatitude[0].firstChild.data)
         }
     }    
    }





Similar Threads
Thread Thread Starter Forum Replies Last Post
Help: Arrays using JavaScript,/XML in IE celticbhoy Ajax 0 December 10th, 2007 01:15 PM
iterator..... s Larryz C# 2005 0 May 9th, 2007 04:06 PM
Comparing arrays in javascript JohnD Javascript How-To 0 August 29th, 2004 02:32 PM
Accessing of Arrays of jsp in javascript theguardian@vsnl.net JSP Basics 1 August 26th, 2004 11:16 PM
Accessing of Arrays of jsp in javascript theguardian@vsnl.net JSP Basics 0 August 26th, 2004 12:54 PM





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