|Feb 2, 2013, 09:15 AM||#1|
Safari Bookmarklets Modulo Bug + Workaround
I am putting this here so that Google will index it, since I don't have any blog of my own to put it on. It's new information that I did not see anywhere else on the net.
Basically. Safari hates the use of Modulo (%) in bookmarklets.
The error looks as follows:
It took me enough time to track down that I figure it's worth sharing.
If you look on the 4th line from the bottom, you will see "ruleNum%2==1" which is a common way of using modulo. Basically, it divides ruleNum by 2, which means that we'll get a remainder of either 0 or 1. If it's 0, it's an even number, if it's 1, it's an uneven number. People often use modulo to determine the even/odd rows of a sequence of numbers.
Well, there's just no way, WHATSOEVER, to use Modulo in bookmarklets. You can try assigning it to variables. You can try wrapping it in functions. It will just not work.
Luckily, if you only need to detect even/odd numbers there is an alternative method, using Binary AND. It works through the fact that binary numbers end in 0 if they are even and 1 if they are odd.
Safari allows the Binary AND (&) operator, but not the Modulo (%) operator.
Using Binary AND, we can "mask out" the given number, keeping only the last bit, and checking if it's 0 or 1 to determine if it's an odd or even number.
So, here's the solution:
Checking for Even numbers:
FORBIDDEN: (num%2 == 0)
ALLOWED: (num&1 == 0)
Checking for Odd numbers:
FORBIDDEN: (num%2 == 1)
ALLOWED: (num&1 == 1)
Enjoy, Google visitors of the future! The only caveat to remember is that this trick works perfectly for INTS (whole numbers like 0, 4, 10, 5034), but that extra care must be taken if using floating point numbers (5.414, 2.103, etc), such as Math.round()/Math.floor()'ing them to an integer first.
An added bonus is that the AND-method is twice as fast as MOD, but then again nobody really cares about speed for a bookmarklet. ;-)
I now take my bow.
Last edited by aitte; Feb 2, 2013 at 09:30 AM.
|bookmarklets, browser bugs, modulo, safari|
|Thread Tools||Search this Thread|
|thread||Thread Starter||Forum||Replies||Last Post|
|Safari Weird Bug & Download folder Bug||DJTaurus||OS X Mavericks (10.9)||0||Feb 18, 2014 05:11 PM|
|Modulo and Units...||ArtOfWarfare||Mac Programming||7||Jan 29, 2014 01:17 PM|
|Did anyone find a workaround for the iTunes music bug?||tymaster50||iOS 7||14||Jul 5, 2013 11:27 PM|
|Safari Workaround for MSIE?||mollyparrot||Mac Basics and Help||0||Jul 4, 2013 02:49 AM|
|Chrome: how to use bookmarklets?||piecloud||iPhone and iPod touch Apps||2||Jun 30, 2012 11:12 AM|
All times are GMT -5. The time now is 12:42 AM.