tags:

views:

41

answers:

3

I would like to have a link with looks something like this: Call to Action. However, if the javascript hasn't loaded completely and a user clicks that link, they will be redirected to a php script which spits out json. Alternatively, I will likely do something along the lines of Call to Action.

Is this the best practice for handling this situation?

+1  A: 

The easiest solution is to put the data there with Javascript and then there is no timing mismatch.

<a id="link123" href="#">Call to Action</a>

with, say, jQuery:

$(function() {
  $("#link123").click(function() {
    // get JSON, put the address ehre
  });
});
cletus
Returning false after the click event would be helpful too, it would prevent the browser from jumping to the top of the screen.
Sam152
I wouldn't use a # as an href, ever. Use href="javascript:void(0)" or href="javascript://" ... that way nothing happens at all if the user clicks before the event listener is added.
Robusto
So just store the data in the ID and then match it out? Doesn't seem very elegant
A: 

I think I'm going to go with the metadata plugin for jquery.

http://plugins.jquery.com/project/metadata

Any reasons why this is a terrible idea?

A: 

Ideally, link to a document which is the same as the current document would be after the transformation.

It sounds like you are already overriding the normal link action, so (as far as the client is concerned) you probably just need to do something like adding &type=json to the URL before running it through XHR.

David Dorward