views:

51

answers:

3

Not sure how to phrase this question exactly, but I'll give it a shot. (If you have a suggestion for how to phrase it better so others can find it, tell me and I'll edit it).

I want to know the best practices for handling clicks on a page that are then processed using AJAX. I have been temporarily using code that looks like this in the HTML:

<a href="#clickable">Click me!</a>

And in jQuery I handle the clicks by binding click to the href.

Of course, I know that you should not be making this known to the user.

So how should I be handling these clicks?

A: 

I'm not very sure to understand your question too xD

You can handle a click by doing something like

$("a").click(function() {
  // do some stuff
});

Does this answer to your question ?

Edit : Ok.

You just need to do something like this :

<a href='link' onClick='return MyClass.myFunction()'>keyword </a>

If I good remember, you return FALSE in JS to stay on the same page (and do some process with Ajax). If if JS is not activated, PHP will be used ;-)

Vinzius
Not exactly. Essentially I'd like to be able to have the link do processing in PHP if JS is not enabled. So I would really like to have the `<a/>` tags be the primary means of interfacing with the code.
Josh Smith
Heeeeeeiiin ok. I edited
Vinzius
By the way, how do you handle the "<" caracter on Stack in the <pre></pre>?
Vinzius
Add four spaces before a line to make it format like code. To do this quickly, select a block an hit `Ctr-k`.
Peter Ajtai
@Josh - Take a look at my answer. - It includes functional PHP links if JS is off, and the JS kicks in and overrides the links if it is on.
Peter Ajtai
+1  A: 

You should make your HTML so that it works if Javascript is off. Don't mix content with functionality, so inline Javascript is usually not a good solution.

So, in your case:

<a href="some/working/path.php" class="ajax">Meaningful description!</a>

Then you can add a class (or check the text inside, or something else) to handle it on the Javascript side.

Make sure to use event.preventDefault() to stop the page from changing if Javascript is enabled.

So the jQuery would be

  // Target only anchors with class "ajax"
$("a.ajax").click(function(event) { 

    // Handle AJAX etc here.
    // ...

    event.preventDefault(); // <== Don't navigate away from page
});
Peter Ajtai
Yeah, I do any inline JS, but doing this by classes certainly seems reasonable. Is there any reason why I've usually seen `e.preventDefault();` positioned prior to any other code in a fn?
Josh Smith
@Josh - It doesn't matter whether it's at the end or beginning - whatever you find clearer. It prevents the standard action. The first argument of the callback is the event, so you can call it anything you want or just use `arguments[0]`.... I used `event` just to be clear, but you could do `.click(function(e) { e.preventDefault(); ... })` or `.click(function() { arguments[0].preventDefault(); ... })` or whatever you want to name it.
Peter Ajtai
Thanks. I've used it all the time but didn't realize that it would get called just the same if it came after other code. It'd still be clearer to me personally to put it first. But still good to learn.
Josh Smith
A: 

I usually have the href link to a static php page, and then use javascript to change the link href to the ajax processing page, then you can reference it in the ajax call.

Liam Bailey