views:

420

answers:

4

I'm trying to open the zip code lookup page on the usps website and fill out the form using javascript, and I get the following error: 'Permission denied to get property Window.document'

Here's my script:

function lookupZipCode(line1, line2, city, state, zip) {
  var usps = window.open('http://zip4.usps.com/zip4/welcome.jsp');
  usps.document.getElementById('address2').value = line1;
  usps.document.getElementById('address1').value = line2;
  usps.document.getElementById('city').value = city;
  usps.document.getElementById('state').value = state;
  usps.document.getElementById('zip5').value = zip);
}

I understand that this is a security feature. Ideally I would use query string params but the page does not appear to support them.

Anyone have any ideas for a workaround?

+2  A: 

You should be using the APIs they provide instead of trying to automate filling in a form.

Chad Birch
Your probably right. But, this is what the users asked for and much simpler than using the API. The users are currently using copy and paste to fill out the usps form.
Seth Reno
+4  A: 

If you don't like the API (;-)), you can use the query-string params: http://zip4.usps.com/zip4/welcome.jsp?address1=somethingelse&address2=something&state=FL&city=Miami&zip5=123456789&urbanization=

Rafael
Note that this only works if the user has Javascript enabled. I had NoScript blocking usps.com so this left the form blank for me.
Chad Birch
How did you determine the names for the query string params?
Seth Reno
@Chad Birch: yes, the fields are being filled by JavaScript.
Rafael
@Seth Reno: fill all fields, click "Find state abbreviation", choose something from abbrevation list and the main page reloads. Check the URL ;-)
Rafael
A: 

It would be helpful to know about the security error you received. It's a result of the "same origin policy" for web scripting.

artlung
A: 

I havent worked with it too much but you could use the HTML selector in Yahoo Query language (http://developer.yahoo.com/yql/) to get the parts you need. you can then pass it along as JSON which you can get cross domain.

Kru