Reputation: 101
I am new at using javascript, this is code that someone put into a C# ASP.Net website, It is supposed to get the city and state from a zip code entered. It works great in IE9, Chrome, Firefox, but fails in IE8. I have no idea as to why. Here is the javascript, I hope something stands out to someone. I also included the markup that uses it. I don't know where it is failing, I get the Pcodefail result and '* Unable to find Postal Code' is displayed.
<script type="application/javascript">
$(document).ready(function () {
$('#Pcodefail').hide();
});
</script>
<script type="application/javascript">
$(document).ready(function () {
$('[id$=uxPostalCode]').focus(function () {
var city = $('[id$=uxCityState]');
city.val('');
});
$('[id$=uxPostalCode]').blur(function () {
var city = $('[id$=uxCityState]');
var oCity = $('[id$=uxCity]');
var oState = $('[id$=uxState]');
$.getJSON("http://www.geonames.org/postalCodeLookupJSON?&country=US&callback=?", { postalcode: this.value }, function (response) {
//$.getJSON("http://api.geonames.org/findNearbyPostalCodesJSON?postalcode=?&country=US&radius=10&username=demo", { postalcode: this.value }, function(response) {
if (!city.val() && response && response.postalcodes.length && response.postalcodes[0].placeName) {
city.val(response.postalcodes[0].placeName + ', ' + response.postalcodes[0].adminCode1);
oCity.val(response.postalcodes[0].placeName);
oState.val(response.postalcodes[0].adminCode1);
$('#Pcodefail').hide();
}
else {
$('#Pcodefail').toggle();
}
});
});
});
</script>
HTML
<div class="base-container-controls-100pct">
<div class="base-container-controls-20pct">
<div class="label-sm">Postal Code<span style="color: #ff0000; vertical-align: top">*</span> </div>
</div>
<div class="base-container-controls-50pct">
<asp:TextBox ID="uxPostalCode" CssClass="textbox-sm" Text="" TabIndex="7" runat="server" MaxLength="11"></asp:TextBox>
<asp:RequiredFieldValidator ID="uxreqValZip" runat="server" ErrorMessage="Postal Code" Font-Bold="False" Text="*" ValidationGroup="one" ControlToValidate="uxPostalCode"></asp:RequiredFieldValidator>
<div id="Pcodefail" style="color: Red">* Unable to find Postal Code</div>
</div>
</div>
Upvotes: 1
Views: 318
Reputation: 53311
I'm not 100% sure, but upon a cursory inspection of the code, the first thing that stands out is the tag <script type="application/javascript">
. I'm fairly sure it should be <script type="text/javascript">
instead. IE is very picky about JS tags.
Upvotes: 4