Tag: Code
function getViewport(){
var e = window, a = 'inner';
if(!('innerWidth' in e)){
var t = document.documentElement
e = t && t.clientWidth ? t : document.body
a = 'client';
}
return {width: e[a+'Width'], height: e[a+'Height']}
}
Modified slightly from a comment here.
Edit: the code in that comment didn't fully implement the original, and broke when I tried it in IE. So I've updated the code above.
Re: translate Perl diamond operator to Ruby.
Perl:
while(<>){
...
}
Ruby:
ARGF.each do |$_|
...
end
At this point I question whether I'd ever use Perl for anything again. Until now, Perl filled a niche where if the code I wanted to write would fit in 10 lines or so, and did a lot of string manipulation, I'd turn to Perl. Otherwise Python. Now I think I'll just use Ruby for everything 
Some more handy Javascript (depends on Prototype):
function toggleByClass(id, className, callback){
// shows the element with id 'id'
// and hides all other elements with class 'className'
$A($$('.'+className)).each(function(element){
$(element).style.display = "none";
})
if(id) // call it with a false id to hide all
$(id).style.display = "block";
if(callback)
callback(id, className);
}
function toggleById(id1, id2){
var a = $(id1).style;
var b = $(id2).style;
if(a.display == 'none' || a.display == ''){
a.display = 'block';
b.display = 'none';
}else{
a.display = 'none';
b.display = 'block';
}
}
List<Type> temp = list.ConvertAll<Type>(delegate(Type a){return a;});
You'd think a generic type like List would have a useful clone or copy method, but noo. (Type is just a placeholder... insert your type here)
-
I dunno if I have a recent version of my blogging bookmarklet on my site anywhere... last time I tried to find it I think the version I found was outdated. So here it is: Blog.
¶ (0)
Tags: [Bookmarklets, Code, This website]
-
Because I'm sure it'll come in handy later, here's a javascript object clone function:
function clone(obj){
if(obj == null || typeof(obj) != 'object')
return obj;
var temp = {};
for(var key in obj)
temp[key] = clone(obj[key]);
return temp;
}
Update: From feedback in the comments, a better version is:
function clone(obj){
if(obj == null || typeof(obj) != 'object')
return obj;
var temp = new obj.constructor(); // changed (twice)
for(var key in obj)
temp[key] = clone(obj[key]);
return temp;
}
¶
Tags: [Code, Javascript]
results = commands.collect{|i| host.cmd(i)}
.collect{ |i| i.collect{ |i| i.chomp }[1..-2] }
(reformatted slightly)
What this code does: takes a list of commands to run over a telnet session, runs them all, takes the output for each command, splits it into an array of lines (removing any stray newlines on each line), strips off the first and last lines of the output (since the first line is just an echo of the command and the last line is the command prompt again), and returns an array of lines of the output for each command in an array.
def isPowerOf2(num):
return num == 1 or num == 2 or (num%2 == 0 and isPowerOf2(num/2))
def isPowerOf2(num):
count = 0
while(num > 0):
count += num%2
num >>= 1
return count == 1
Update: From David, in the comments:
def isPowerOf2(num):
return (num & (num - 1) == 0)
I've never gotten to do enough bit twiddling to have let me think of that.
-
Here's some quick Perl I used to unescape some form-encoded data:
while(<>){
s/\+/ /g;
s/%([0-9A-Fa-f]{2})/chr(hex($1))/ge;
print;
}
¶ (0)
Tags: [Code, Perl]
|
Generated in about 0.166s. (Used 10 db queries) |
new⇒The Elegant Universe
Well I have finally found the crazyguy that preaches useless nonsencein A...
Joseph Baxter: Jan 7, 11:07pm