www.webdeveloper.com
Results 1 to 14 of 14

Thread: onMouseDown event help

Hybrid View

  1. #1
    Join Date
    Sep 2013
    Posts
    7

    Unhappy onMouseDown event help

    Hi guys,


    Im having some problem with onMouseDown event.


    So like when you go to Google and click on a search result... something like this gets prefixed to the landing page:


    Code:
    http://www.google.co.in/url?sa=t&rct=j&q=&esrc=s&source=web&cd=2&ved=0CDcQFjAB&url
    My script:


    Code:
    $(document).ready(function () {
      var count = 0;
      var matchavailable = 0;
      var protectedlinks = "google.com,yahoo.com";
      $("a").mousedown(function () {
      var linkArray = protectedlinks.split(',');
      for (var i = 0; i < linkArray.length; i++) {
      if ($(this).attr('href').indexOf(linkArray[i]) > 0) {
      matchavailable = 1;
      break;
      }
      else {
      matchavailable = 0;
      }
      }
    
    
      if (matchavailable == 0) {
      if (count == 0) {
      $(this).attr('href', "http://localhost/wordpress/redirect.php?link=" + encodeURIComponent($(this).attr('href')));
      $(this).attr('target', '_blank');
      count = count + 1;
      }
      }
    
    
      });
    
    
    
    
    });
    This is exactly what I've been trying to do. However, with this script, the problem is -

    Code:
    http://localhost/wordpress/redirect.php?link=
    part gets prefixed on only one link.


    Suppose I have a lot of different website links like bing.com, webdeveloper.com...the thing is, if I click on bing.com I get that redirect.php prefixed. However, right after that, when I try to do it on webdeveloper.com link... nothing gets prefixed (and vica versa). What am I missing?


    Hope I've been descriptive enough

  2. #2
    Join Date
    Jul 2011
    Posts
    131
    It seems like after click on the first link variable count becomes equal 1 and this condition
    Code:
    if (count == 0) {
      $(this).attr('href', "http://localhost/wordpress/redirect.php?link=" +   encodeURIComponent($(this).attr('href')));
      $(this).attr('target', '_blank');
      count = count + 1;
    }
    will never be true for all next links. And so any other of your links will not be prefixed.

  3. #3
    Join Date
    Sep 2013
    Posts
    7

    Lightbulb

    Hi Kromol,

    Thanks for the help.

    I have modified the coding to:

    Code:
    $(document).ready(function () {
    
        var matchavailable = 0;
        var protectedlinks = "facebook,google";
        $("a").mousedown(function () {
            var linkArray = protectedlinks.split(',');
            for (var i = 0; i < linkArray.length; i++) {
                if ($(this).attr('href').indexOf(linkArray[i]) > 0) {
                    matchavailable = 1;
                    break;
                }
                else {
                    matchavailable = 0;
                }
            }
    
            if (matchavailable == 0) {
                if ($(this).hasClass('linked')) {
                }
                else
                {
                   $(this).attr('href', "http://localhost/wordpress/redirect.php?link=" + encodeURIComponent($(this).attr('href')));
                    $(this).attr('target', '_blank');
                    $(this).addClass("linked");
                }
                 
                
            }
    
        });
    
    
    });
    The script seems to be working fine so far on my localhost.
    However, when i put the same on the server to test it out on my wordpress site, it doesn't seem to work.
    In spite of my site loading jquery and this script, there's no effect.

    This is the error that I get:

    Error: ReferenceError: $ is not defined

    It's working fine as intended on localhost.. but on my the server? What gives?

  4. #4
    Join Date
    Jul 2011
    Posts
    131
    You have to load jQuery before loading your script

  5. #5
    Join Date
    Sep 2013
    Posts
    7
    Sweet! that solved it.

    Thanks a lot!

  6. #6
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,648
    another one

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title>Prefixed links</title>
    <script src="http://code.jquery.com/jquery-latest.js"></script>
    <script>
    $(document).ready(function(){
    var linkArray=['google.com','yahoo.com'];
    $('a').mousedown(function(){
    var hrf=$(this).attr('href'),domen=hrf.substr(7);
    domen=(domen.indexOf('/')>0)?domen.substring(0,domen.indexOf('/')):domen;
    if(jQuery.inArray(domen,linkArray)<0){
    $(this).attr({href:"http://localhost/wordpress/redirect.php?link=" + encodeURIComponent(hrf),target:'_blank'});
    }
    });
    });
    </script>
    </head>
    <body>
    <a href="http://google.com">link 1</a><br />
    <a href="http://yahoo.com">link 2</a><br />
    <a href="http://yandex.ru">link 3</a><br />
    <a href="http://microsoft.com">link 4</a><br />
    <a href="http://www.webdeveloper.com/forum/showthread.php?283531-onMouseDown-event-help&p=1286813#top">link 5</a><br />
    </body>
    </html>
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

  7. #7
    Join Date
    Sep 2013
    Posts
    7
    ...And I'm back with another small query!

    So basically, the javascript seems to be working great on all anchor tags. I just realized that I have a popup on my site with a cross (X marks the spot) at the corner of the popup to close it.

    The problem is, the popup X mark's code starts like:

    Code:
    <a href="javascript:hide_popup('deal_popup');popup_close_cat();">
    When I try to close it, instead of the popup getting closed, I get something like:

    Code:
    <a href="http://domain/redirect.php?link=javascript:hide_popup('deal_popup');popup_close_cat();">
    ...thereby not letting me close it. Is there any way to NOT allow the javascript to run on such anchor tag?

    Code:
    var matchavailable = 0;
    var disallowinks = "google,microsoft,yahoo";
    $n("a").mousedown(function () {
        var linkArray = disallowlinks.split(',');
        for (var i = 0; i < linkArray.length; i++) {
            if ($n(this).attr('href').indexOf(linkArray[i]) > 0) {
                matchavailable = 1;
                break;
            }
            else {
                matchavailable = 0;
            }
        }
    
        if (matchavailable == 0) {
            if ($n(this).hasClass('linked')) {
            }
            else
            {
               $n(this).attr('href', "http://yoursite.com/redirect.php?link=" + encodeURIComponent($n(this).attr('href')));
               $n(this).attr('target', '_blank');
               $n(this).addClass("linked");
            }
    
    
        }
    
    });
    Thanks for the help I've received previously. Can't wait to solve this one!

  8. #8
    Join Date
    Jul 2011
    Posts
    131
    Add "javascript:" to disallowinks variable.

  9. #9
    Join Date
    Sep 2013
    Posts
    7
    I actually tried

    Code:
    var disallowinks = "google,microsoft,yahoo, javascript and/or javascript:";
    But that didn't work at all.

    I think we have to do it via some other method.

  10. #10
    Join Date
    Jul 2011
    Posts
    131
    You have space between last comma and "javascript". Is it a typo?
    Or have you tried this:
    Code:
    var disallowinks = "google,microsoft,yahoo,javascript:";

  11. #11
    Join Date
    Sep 2013
    Posts
    7
    yes, I have tried that code but didn't work. Still trying to get a workaround for this

  12. #12
    Join Date
    Jul 2011
    Posts
    131
    I think I see your issue. indexOf returns -1 if it cannot find substring, in other case the position of the first occurence.
    In JavaScript strings starts from index 0.
    This code:
    Code:
     if ($n(this).attr('href').indexOf(linkArray[i]) > 0) {
    will not work if substring starts at the beginning of a string.
    You have also to check for index 0.
    Code:
     if ($n(this).attr('href').indexOf(linkArray[i]) >= 0) {
    And don't forget this
    Code:
    var disallowinks = "google,microsoft,yahoo,javascript:";

  13. #13
    Join Date
    Sep 2013
    Posts
    7
    Sweet. That did the trick. Thanks a lot for that along with explanation

  14. #14
    Join Date
    Jul 2011
    Posts
    131
    You are welcome

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles