Jquery disable links

  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Posts: 3422
  • Loc: Richland, WA

Post 3+ Months Ago

Right now Im trying to implement some ajax into one of my websites However, to be kind to those that run with javascript disabled I left my links intact. Now what I need to do is make it so when a user that has javascript enabled comes to my site I want to disable all the links. Right now I have a script setup that will change all href attribute to a hash (#). The problem doing this is that I don't want to destroy the link (for cases of bots browsing the site that execute javascript).

Code: [ Select ]
//Submit vote for dislike
    $("a.dislike").each(function(){
        $(this).attr('href', '#dislike');
        $(this).click(function(){
            $.post("/index.php/dislike", { ajax_request: "1", post_id: $(this).attr("name") },
                function(data){
                    if(data != "false")
                    {
                        var current_value = $("#dislike_" + data).html();
                        current_value++;
                        $("#dislike_" + data).html(current_value);
                        // Change value
                    } else {
                        // Do nothing
                    }
                       
                });
        });
    };
  1. //Submit vote for dislike
  2.     $("a.dislike").each(function(){
  3.         $(this).attr('href', '#dislike');
  4.         $(this).click(function(){
  5.             $.post("/index.php/dislike", { ajax_request: "1", post_id: $(this).attr("name") },
  6.                 function(data){
  7.                     if(data != "false")
  8.                     {
  9.                         var current_value = $("#dislike_" + data).html();
  10.                         current_value++;
  11.                         $("#dislike_" + data).html(current_value);
  12.                         // Change value
  13.                     } else {
  14.                         // Do nothing
  15.                     }
  16.                        
  17.                 });
  18.         });
  19.     };
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

I don't believe any bots are going to evaluate the entire page script to discover that you're killing off the href attribute. As far as I know they'll only skim the element for onclick attributes or javascript:code in the href attribute.

I'd love to see something that says otherwise though. :)

If you add onclick="return false;" to the links, javascript disabled browsers should ignore it, and javascript enabled browsers should execute your jQuery event listeners and then bail on account of the "return false", leaving your href intact.
  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Posts: 3422
  • Loc: Richland, WA

Post 3+ Months Ago

Awesome, the onclick worked. I was thinking of doing that but I thought jquery would just overwrite it.
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

onclick is a legacy event handler.
jQuery uses the addEventListener/attachEvent (depending on the browser) model provided by the DOM when you use the "click()" and similar jQuery methods.
  • RedBMedia
  • Proficient
  • Proficient
  • User avatar
  • Posts: 315

Post 3+ Months Ago

Whoa! Slow down! Google can now crawl some pretty sophisticated JS. i don't know if they can crawl your example above, but the below article and video shows that they are crawling and analyzing more than inline scripts.

How not to hide text [VIDEO]
Google I/O: New Advances In The Searchability of JavaScript and Flash, But Is It Enough? [BLOG POST]
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

If they're getting the code in that video, I think it's safe to say they're getting anything in JS.

It would explain why Google built their own javascript engine with a focus on efficiency. I bet Chrome was one of those "What else can we do with this engine?" sort of projects.

Post Information

  • Total Posts in this topic: 6 posts
  • Users browsing this forum: No registered users and 145 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.