Archive for the ‘Javascript’ Category

Confirm your email – how to stop copy and paste

Whenever I come across a web-form that asks me to retype my email to confirm it, I copy and paste the email.  Saves me typing, and I’m pretty sure 99.9% of the time I typed it right the first time.

I just came across a form that stopped me pasting into it.  Rather annoying, but could be useful if you really want to force users to do this.

Easily done.  Just add  onpaste=”return false” inside the <input> tag.

 

Get longitude and latitude from Google maps

Need to get the longitude and latitude for a particular place?
Find it on Google maps, double click it to will centre the map where you clicked.
Then paste this in the address bar:

javascript:void(prompt('',gApplication.getMap().getCenter()));

I’ve added this as a bookmarklet for ease of use.

To do the same drag this into your browser toolbar Google Map Centre

 

jQuery and asp.net validation controls

Just found a  nice tip to check with jQuery if all built-in asp.net validation controls on a form are valid.

As an asp.net developer and jQuery advocate I know I’m going to find this tip very useful

http://devoma.com/post/JQuery-and-ASPNET-validators.aspx

 

ASP.NET Event Validation and “Invalid Callback Or Postback Argument”

I was getting this and pulling my hair out.

My button (server-side) was pumping out a bit of javascript 

Page.ClientScript.RegisterStartupScript(GetType(String),
         "loginunsuccessful", "<script type=""text/javascript"">
         alert('Login unsuccessful – please try again');</script>")

And this kept causing the error.

I tried using the prescribed fix: ClientScript.RegisterForEventValidation(mycontrol.uniqueid) in the prerender.  Still got the error.

Here’s how I fixed it.  RegisterStartupScript does the job of running at startup by emiting the js right at the foot of the page.  I wrapped my JS in jQuery’s own onload code.

Page.ClientScript. RegisterStartupScript(GetType(String),
          "loginunsuccessful", "<script type=""text/javascript"">
          $(document).ready(function(){alert('Login unsuccessful – please try again');});
          </script>")

Still the same error, I then changed to using Page.ClientScript.RegisterClientScriptBlock, which emits the JS in the midst of the html and hey presto no errors.  This is without using the RegisterForEventValidation I’d like to add.

Final code:

Page.ClientScript.RegisterClientScriptBlock(GetType(String),
          "loginunsuccessful", "<script type=""text/javascript"">
          $(document).ready(function(){BlockAlert('Login unsuccessful – please try again');});
          </script>")

Simple to fix when you know how.

 

Use jQuery to replace <body onunload="GUnload()">

When using Google maps it’s important to use GUnload on the page’s unload event. Fine when you can access the body tag directly…. But if you have the Google map functionality wrapped inside an ascx you need a different approach.

After a big of digging I found how to use jQuery to “catch” the unload event. In case anyone else needs it:

$(window).unload( function () { GUnload(); } );

 

Microsoft adopt jQuery

I’ve been using jQuery for ages and I love it.  Finally a very easy, crossbrowser, method to target any element or group of elements on the page.

Plus it has some very straightforward and powerful ajax features built in.

I did look at the whole ajax.net thing a while back and it was way too bloated for me to give it a serious look.

I’ve just heard the Microsoft have woken up to jQuery too.  In future versions of Visual Studio jQuery intellisense wil be built it.  Thanks Microsoft!