Forum Moderators: open

Message Too Old, No Replies

Another Syntax Error in IE but not FF but different

Another Syntax Error in IE but not FF but different

         

tiny68

4:49 am on May 17, 2007 (gmt 0)

10+ Year Member



Hi Eveyone,

First off thanks for any help anyone can provide here.

The issue is I needed to lookup a postcode based on city and state form elements on the client side. My solution was to use ASP on the server side to render out a postcode.js file whenever the postcodes table on the DB is updated. The js file is essentially static at any other time. I used the ASP to render out a rather large 2D array which has some 16740 elements in the first dimension and 3 in the second.

This works perfectly well and despite the rather large resulting js function runs very quickly.

By putting it in a *.js include the browser only has to load the file once and it runs very quickly after that. Its the only way I could think of to do it on the client side which I need to do as the form is very complicated and this half way through it. This is not a publicly accessible application so load time is not that important although if there is a more efficient way let me know (Im quite competant on serverside asp etc but my JS skills are not as good).

The error is a "syntax error" on line 32832 which when I view source is:

postcode_info[10918][0] = '4671';
postcode_info[10918][1] = 'ST KILDA';
postcode_info[10918][2] = 'QLD';
postcode_info[10919][0] = '4671';
postcode_info[10919][1] = 'TAKILBERAN'; <-- This line
postcode_info[10919][2] = 'QLD';
postcode_info[10920][0] = '4671';
postcode_info[10920][1] = 'TIRROAN';
postcode_info[10920][2] = 'QLD';
postcode_info[10921][0] = '4671';
postcode_info[10921][1] = 'WALLAVILLE';

Firefox reports no error, no warning and functions perfectly.

The important js is included:

function getPostCode()
{

var result = new String("");
var temp_postcode = new String("");
var temp_city = new String("");
var temp_state = new String("");
var postcode_obj = document.getElementById('recv_postcode');
var city_obj = document.getElementById('recv_city').value

var b = document.connote.recv_state.selectedIndex;
var state_obj = document.connote.recv_state.options[b].value;
var postcode_info = new Array(17000);
for (i=0;i<17000;i++)
{
postcode_info[i] = new Array(3);
}

postcode_info[0][0] = '0200';
postcode_info[0][1] = 'AUSTRALIAN NATIONAL UNIVERSITY';
postcode_info[0][2] = 'ACT';
postcode_info[1][0] = '0221';
postcode_info[1][1] = 'BARTON';
postcode_info[1][2] = 'ACT';
postcode_info[2][0] = '0800';
postcode_info[2][1] = 'DARWIN';
postcode_info[2][2] = 'NT';
.
.
.
postcode_info[16740][0] = '9729';
postcode_info[16740][1] = 'GOLD COAST MC';
postcode_info[16740][2] = 'QLD';

var upper_limit = 16740;

if (document.getElementById('recv_city').value!= '' && document.getElementById('recv_state').value!= '')
{
postcode_obj.value = "";

for (z = 0; z <= upper_limit; z++)
{
temp_postcode = postcode_info[z][0]
temp_city = postcode_info[z][1]
temp_state = postcode_info[z][2]

if (temp_city.toUpperCase() == city_obj.toUpperCase() && temp_state.toUpperCase() == state_obj.toUpperCase())
{

result = temp_postcode;
postcode_obj.value = result;
}
}
}
}

looking forward to hearing how silly an idea this was :D

Fotiman

3:35 pm on May 17, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Here's how I might do it instead. Note, I've removed the "new" statements (they're not efficient). I've also changed the way the postcode_info array gets populated, so you would probably need to tweak your ASP to output in this way instead (shouldn't be too difficult). I've also added some additional checks in some places (don't assume that your select element will have a selectedIndex for example).


function getPostCode()
{
var result = "";
var temp_postcode = "";
var temp_city = "";
var temp_state = "";
var postcode_obj = document.getElementById('recv_postcode');
var recv_city = document.getElementById('recv_city');
var recv_state = document.getElementsById("recv_state");
var city_obj = recv_city.value
var state_obj = "";
if (recv_state.selectedIndex >= 0) {
state_obj = recv_state.options[recv_state.selectedIndex].value;
}
var postcode_info = [
['0200', 'AUSTRALIAN NATIONAL UNIVERSITY', 'ACT'],
['0221', 'BARTON', 'ACT'],
['0800', 'DARWIN', 'NT'],
['9729', 'GOLD COAST MC', 'QLD']
];
if (city_obj!= '' && state_obj!= '') {
postcode_obj.value = "";
var upper_limit = postcode_info.length;
for (var z = 0; z < upper_limit; z++) {
temp_postcode = postcode_info[z][0];
temp_city = postcode_info[z][1];
temp_state = postcode_info[z][2];
if (temp_city.toUpperCase() == city_obj.toUpperCase() && temp_state.toUpperCase() == state_obj.toUpperCase()) {
result = temp_postcode;
postcode_obj.value = result;
}
}
}
}

Also, I suspect this is where your syntax error really is (which I've corrected in my example above):


document.getElementById('recv_state').value!= ''

tiny68

12:30 am on May 18, 2007 (gmt 0)

10+ Year Member



You sir are an absolute legend :D

I did have some problems with the way you grabbed data from the recv_state form element (which is a select list) but I adapted your example slightly and got it working.

Your method of building the array reduced the function size from 1.8MB to approx 625KB so that's awesome :) Best of all it works in IE as well now..

Thanks heaps