1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

If (on match OR on (click)) { // do something } ? Possible?

Discussion in 'jQuery' started by PoPSiCLe, Apr 4, 2015.

  1. #1
    As you can tell by the title - I'm looking for a way to do both a check for click() and whether or not a variable (a counter) reaches 0 - it's for a logout-function, and instead of repeating code or fetching some function within the function after click(), I'm wondering if it's possible to run a click-event inside an if().

    I can provide code if needed, but right now I'm just looking for some ideas on how to achieve what I'm after, or someone pointing me in the right direction.
     
    PoPSiCLe, Apr 4, 2015 IP
  2. Alexstorm

    Alexstorm Greenhorn

    Messages:
    32
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    18
    #2
    Not sure if this is helps:

    JQuery example: I wanted to check for a plug in and had to look through them all, then give a var result value. Based on that result, I can then do If or Else.

    This checks to see if Google maps are an active browser plugin on the click on something in the div id="check_plugin".

    It's doesn't do what you are looking for, but it does iterate through data, gets a result and then acts accordingly.

    
    $("#check_plugin").click(function() {
        var L = navigator.plugins.length;
        for(var i = 0; i < L; i++) {
          if (navigator.plugins[i].filename == "npgeplugin.dll") {
                var ecount = 1;
                }
         }
          if (ecount == 1) {
                // Do something if Plugin is found
                }
            else {
                // Do something if Plugin is missing
                } 
    });
    
    Code (JavaScript):
     
    Alexstorm, Apr 4, 2015 IP
  3. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #3
    Yeah, I know I can do it like that - seems to be the only way - I was just wondering if there was some easier way :)
     
    PoPSiCLe, Apr 5, 2015 IP
  4. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #4
    If both are supposed to do the same action, make one function that does said action, attach it via the click event to your anchor (assuming it's an anchor and you aren't pissing on semantics using BUTTON for non-form stuff) and to your event timer.

    Doesn't sound like something hard, sounds like you are trying to make it hard. Though making something simple needlessly complex seems to be what jQuery is for...

    ... and damn, that jQuery crap @Alexstorm posted; proof of what uselessly cryptic garbage jQuery is and why I don't poke my head into this part of the forums unless I'm really bored -- and only do so to typically ask "how is that making it any better?"
     
    deathshadow, Apr 15, 2015 IP
  5. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #5
    It's a submit, actually - and it's within a form, no worries, it works fine without javascript as well. Could probably be a link as well, although I prefer having an actual post-processing going on. I ended up not rewriting the code for now, I need to look into some other handling functions first, then I can probably just reuse those.
    I don't really understand how you think jQuery is more cryptic than regular javascript, however - the jQuery code above is very easy to read - it's a click-event on the given ID (same prefix for IDs as CSS uses), and it does something on a for-loop - I don't understand how that is in any way cryptic. Personally, I prefer the simple syntax of jQuery to regular javascript, simply because I feel regular javascript becomes an endless statement of "addEventListener" and document.whatever - which to me is more cryptic and bloated when I read through the code.
     
    PoPSiCLe, Apr 17, 2015 IP
  6. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #6
    I started out in a cryptic language (aka machine language) and my first real high level language was Pascal, followed quickly by Modula. I'm a "Wirth kind of guy" and find things like C syntax languages to be needlessly and pointlessly cryptic due to a lack of WORDS and an over-reliance on symbology. Those pesky "words" like "addEventListener" I prefer to see so I can at least have some clue what the hell is going on.

    I see .click, I would assume it is SENDING the click message, not attaching an event handler FOR click just like it actually is on ACTUAL elements; a LOT of jquery's methodologies are counter-intuitive to me because of that. If it was say "addClickHandler" instead of just "click' I'd probably be ok with it.

    I've never understood how people think how verbose names are "cryptic", but random symbols and vague short names/abbreviations are not... Doesnae make the least bit of sense. The laugh being in most cases it's the same amount of code or less functional/harder to follow code, that then needs some fat bloated garbage library to function.

    I still see zero advantages and WAY too many disadvantages; the alleged advantages people keep talking about being more the job of MAYBE a small 10-15k library, not a 100k "framework"; and as always 90% of the crap in the jQuery codebase is either CSS' job or has no damned business on a website in the first place.
     
    deathshadow, Apr 17, 2015 IP
  7. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #7
    If we're discussing stuff like that, why in the world are you using PHP? :-D There's hardly any programming language with less consistency in naming conventions :)
    Personally I think the functions in jQuery makes sense, as long as you remember that each name is prefixed by an "on" - onClick, onEach, onSubmit etc.
    I dunno, personally I've always found regular javascript complicated and overly verbose. I guess it varies based on background.
     
    PoPSiCLe, Apr 18, 2015 IP
  8. Alexstorm

    Alexstorm Greenhorn

    Messages:
    32
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    18
    #8
    Hey, guys, my crap example actually works. It runs through the plugins, checks if npgeplugin.dll is one of them and returns a single result, it's one of the plugins, or not. I am waiting to see the same thing in Javascript. I want to learn the best way to do it. Thanks.
     
    Alexstorm, Apr 18, 2015 IP
  9. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #9
    Well, I was more complaining about the jQuery than the code itself; but it has logic flaws too -- like that you keep parsing even once the element is found; short circuit evaluation exists for a reason.

    function eventAdd(e, name, func) {
    	if (e.addEventListener) e.addEventListener(name, func, false);
    		else e.attachEvent('on' + name, func);
    }
    
    eventAdd(document.getElementById('check_plugin'), 'click', function(e) {
    	var i = navigator.plugins.length;
    	while (i--)  if (navigator.plugins[i].filename == 'npgeplugin.dll') {
    		// Plugin found action here
    		return
    	}
    	// Plugin not found action here
    });
    Code (markup):
    Same thing, no jquery... short circuit eval so it doesn't keep looping when the result has already been determined. Requires one small library function, but means you don't need the fat bloated train wreck known as jQ. Sure it's 25 more bytes (oh noes), but is saving 25 extra bytes worth the ridiculous 100k of library nonsense AND making the code harder to follow / needlessly and pointlessly cryptic?

    IF you wanted to use the result based method (for christmas knows what reason) retaining the for loop, one should still short-circuit eval out.... in your case a BREAK would do the job. I'd probably also define ecount BEFORE the loop as zero/false since you don't want it accidentally picking up a global with the same name.

    	var eCount = false;
    	for (var i = 0; i < L; i++) {
    		if (navigator.plugins[i].filename == "npgeplugin.dll") {
    			ecount = true;
    			break;
    		}
    	}
    	if (eCount) {
    		// Plugin found action here
    	} else {
    		// Plugin not found action here
    	}
    
    Code (markup):
    Once you KNOW the value is present, STOP LOOPING. Still, that feels quite inefficient since it's basically extra variables AND extra logic for nothing. Seems like a LOT of people these days are throwing extra logic and variables at problems and I still can't quite decipher why. (see all the jacktard nonsense people do with $_GET, $_PUT or worse SQL result sets in PHP)

    Oh, and I'm getting in the habit lately of parsing arrays backwards -- it's pretty much the same speed; faster in fact if you use the inverted "index--" compare, (remember 0 == false), and means if you want to unset values from the array you don't have to worry about the re-index issue. One of those times where understanding the difference between --i and i-- really pays off.
     
    deathshadow, Apr 18, 2015 IP
  10. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #10
    Oh, one other cute way it could be handled:
    	var i = navigator.plugins.length;
    	while (0 < i--) if (navigator.plugins[i].filename == 'npgeplugin.dll') break;
    	if (i < 0) {
    		// Plugin not found action here
    	} else {
    		// Plugin found action here
    	}
    Code (markup):
    using i-- that way if it's not found, the index will become -1. If it is found, the index will not just be non-negative, it will also be able to be used as a pointer into ::plugins if desired.
     
    deathshadow, Apr 18, 2015 IP
  11. Alexstorm

    Alexstorm Greenhorn

    Messages:
    32
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    18
    #11
    Thanks DS. Looks good. I think you may run into the issue I found. Say the plugin is 5 down in the list of plugins. If you don't go through the entire list, then the gives immediately a not found for the first plugin and goes to the else. What I was adding up the results of checking all the plugins 0 + 0 + 0 + 0 + 1, remember it is the 5th in the list. I used a var ecount gets converted to = 1, then the success. So your code does this with just while (0 < i--)? If so, that is a nicer way.
     
    Alexstorm, Apr 20, 2015 IP
  12. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #12
    I'm not sure what issue you are referring to --- the first example the not found condition is outside the loop and the found does a return. The second +version that returns a value if it were the fifth item my code would return 4. If it's the first item then after the loop i would be zero. The "false' condition is a -1.

    You ever notice how some of the JavaScript functions return -1 on a false condition instead of boolean false? same reasoning. In the 'found' condition of either way I wrote that, you could use navigator.plugins to access the positive result object if desired in the true condition; either will return the index if found, -1 if not found.

    Even the "break" from loop version will give you true or false instead of the one or "unset" condition you seemed to be using. It just short-circuits out on positive with the reasoning "why keep looping if we already found it?"

    remember, variable-- decrements the variable but returns it's value BEFORE it was decremented, so that while is the same as writing:

    while (i>0) {
      i--;
      /* run the test here */
    }
    Code (markup):
    If the array was five items long, i would start at 5. if (5) is true, so i gets decremented to 4 BEFORE we check, since arrays are indexed by 0 and not 1, the last item is #4. Next pass is a "if (4)", which is also true. The if(0) will still run the -- despite 0 being false, resulting in it exiting in a negative 1 condition.

    It is actually far simpler even if it is going through the list backwards, and means two less variables since we don't need length stored separate and we can use the index as the result instead of having that eCount variable.

    I don't know people are being taught to use extra variables for nothing, or if it's just copypasta or not thinking about it, but the past two or three years it's like everyone's hopping on the bandwagon of using complex logic and extra variables that just aren't needed.
     
    deathshadow, Apr 20, 2015 IP
  13. Alexstorm

    Alexstorm Greenhorn

    Messages:
    32
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    18
    #13
    Great, Thanks. I just used the extra variable, because I didn't know this solution. If you have a favorite blog that chats about these kind of better and tighter code solutions, please let me know.
     
    Alexstorm, Apr 21, 2015 IP