Showing posts with label javascript. Show all posts
Showing posts with label javascript. Show all posts

Monday, 23 November 2009

Hou do you get a hyperlink to run some code instead of linking to another location?

I was just working on a web page today and the design was such that there were multiple hyperlinks, two were to other pages but the third was a function to validate a portion of the current page. My problem was, how do I run some javascript instead of setting the anchor to another page?

I first tried to use an onclick handler without a url - now this did work but as a side effect of not having a url link the hyper link was not drawn showing the underline and coloring. So I added a url but of course the onclick functioned correctly but the page then navigated away.

Finally, and successfully, if you specify javascript:functionname() in the anchor href- where functionname() is defined in the script somewhere in your html document it works a charm. Additionally, after the javascript: you actually add multiple commands as long as they are semi-colon delimited. Obviously this can get a bit messy so better to have all the code in the function but the option is there!

Thursday, 8 October 2009

How and why do you url encode and decode?

Well less of me and more of a guy called Brian Wilson. I had a file that I wanted to url encode - since the filename I had used was littered with spaces and at the time couldn't precisely remember what to change!

Anyway to cut a long story short if you want to know the whys and hows or url encoding go visit Brian's page - http://www.blooberry.com/indexdot/html/topics/urlencoding.htm

The cool thing is he has placed some code on the site so you can actually encode your unencoded url! Very useful, giving you the details of the JavaScript functions String.charCodeAt and String.fromCharCode.

Friday, 21 August 2009

How do you populate a new email from an HTML onclick code?

I have used HTML links before with the href set to mailto:address@blah.com, which always work a treat. Today though needed the same functionality as a straightforward mailto link but via an onclick event of a DIV I'm using as a button.

Not difficult once you know how, and I also found out a couple of neat new tricks for populating the mail client with values.

All the onclick code requires is:

onclick="self.location = 'mailto:help@selfhelpsupemarket.com'"

This works just as a normal mailto. What I found out looking this up is you can pre-populate the cc, bcc, subject and the message body as well which coule be very useful! I imagine the following works for a normal link as well as the onclick, additionally the testing I did was with an Outlook 2007 client so I don't know how other clients will behave but here's the code anyway!

Simply append to the mailto: email address a query string using cc, bcc, subject and or body fields to pre-populate the client mail message with the appropriate fields filled. All the fields are optional so you only need to use the fields appropriate to your task. All fields and values need to be separated with ampersand.

A couple of examples:

mailto:steve@t.com?subject=hello from website&cc=info@test.com

So create a new email to be sent to steve@t.com, cc'd to info@t.com with a subject of hello from website.

mailto:steve@t.com?subject=hello from website&body=Dear Steve,

This one will create a new message again to steve@t.com with the same subject but the message body is started with Dear Steve.