Skip to content

Instantly share code, notes, and snippets.

View ryanhanwu's full-sized avatar
🎯
Focusing

Ryan Wu ryanhanwu

🎯
Focusing
View GitHub Profile
@ryanhanwu
ryanhanwu / docwrite.js
Created April 13, 2012 17:11
Do not use document write to create script tag
//Do not use docwrite
document.write('<script src="' + src + '" type="text/javascript"><\/script>'):
//Use Async
var newScript = document.createElement("script");
newScript.async = true;
newScript.src = "http://xxxxxxxxxxxxxxxxxxxx.js";
var s0 = document.getElementsByTagName('script')[0];
s0.parentNode.insertBefore(newScript, s0);
@ryanhanwu
ryanhanwu / cool.js
Created April 18, 2012 08:17
cool usage
//Know arguments number by length
function newFunction(a,b,c,d,e,f,g) {
return false;
}
newFunction.length = 7;
//return function callee for calling
function showSomething(a){
alert(a);
return arguments.callee;
@ryanhanwu
ryanhanwu / __stack.js
Created April 20, 2012 06:40 — forked from bmeck/__stack.js
stack trace api fun for the masses.
var stack_holder = new Error
function stackPrepare(e,stacks) {
var stack = stacks
for(var p in stack[0]) {
stack[p] = stack[0][p]
}
stack.find = function(fn) {
console.log(stack)
for(var i=0;i<stack.length;i++) {
if(stack[i].getFunction() === fn) {
@ryanhanwu
ryanhanwu / run.js
Created April 27, 2012 13:25
JavaScript Scope Chain - Variable
//JavaScript Scope Chain
var mytxt = 'test1';
function echo() {
console.log("A-->" + mytxt);
var mytxt = 'test2';
console.log("B-->" + mytxt);
}
echo();
/*
@ryanhanwu
ryanhanwu / gist:2866961
Created June 4, 2012 07:33
JavaScript Anti-Pattern
* Polluting the global namespace by defining a large number of variables in the global context
* Passing strings rather than functions to either setTimeout or setInterval as this triggers the use of eval() internally.
* Modifying the Object class prototype (this is a particularly bad anti-pattern)
* Using JavaScript in an inline form as this is inflexible
* The use of document.write where native DOM alternatives such as document.createElement are more appropriate. document.write has been grossly misused over the years and has quite a few disadvantages including that if it's executed after the page has been loaded it can actually overwrite the page we're on, whilst document.createElement does not. We can see here for a live example of this in action. It also doesn't work with XHTML which is another reason opting for more DOM-friendly methods such as document.createElement is favorable.
@ryanhanwu
ryanhanwu / ios_note_0626
Created June 26, 2012 02:31
iOS note for cocos2d alert and basic concept
basic concept
http://not-now-nigel.blogspot.tw/2010/07/cocos2d-as-application-framework-part-3.html
subclass uialertview
http://itsmybeats.blogspot.tw/2011/04/custom-uialertview.html
cocos2d alert
http://rombosblog.wordpress.com/2012/02/28/modal-alerts-for-cocos2d/
http://itouchs.blogspot.tw/2011/07/stanford-cs193p-note-04.html
http://ios.biomsoft.com/2011/08/25/custom-dialog-boxes-using-cocos2d-on-iphone/
@ryanhanwu
ryanhanwu / gist:3009037
Created June 28, 2012 04:15
Cell comparasion
Reward* reward = [merchantRewardList objectAtIndex:indexPath.row];
CreditData* currentCreditData = [CreditData dataWithDictionary:[checkInData objectForKey:@"credit"]];
//
[cell setSelectable:YES]; //Every item can be exchange default
//
if ([currentCreditData.points compare: reward.rewardPoints] == NSOrderedAscending) { //Insufficient points
[cell setSelectable:NO];
@ryanhanwu
ryanhanwu / wget.js
Created August 6, 2012 02:28 — forked from xonecas/wget
simple wget for node.js
require('http');
function wget(host, path, https, callback) {
var port = (https) ? 443 : 80,
client = http.createClient(port, host, https),
request = client.request('get', path, {
'host': host
}),
response_body = '';
@ryanhanwu
ryanhanwu / preloadimg.js
Created August 28, 2012 05:02
image preloader
$(function () {
var images = ['image1.jpg','image2.jpg' /* ... */ ];
var imageObjects = [];
var imagesToLoad = 0;
for (i = 0, z = images.length; i < z; i++) {
imageObjects[i] = new Image();
imagesToLoad++;
$(imageObjects[i])
.load(function () {
@ryanhanwu
ryanhanwu / preloaderImg_simple.ks
Created August 28, 2012 05:03
image preloader (do after all loaded)
var images = [ /* you get it by now */ ];
for (i=0,z=images.length;i<z;i++) {
$('<img />')
.attr('src', images[i])
.load(function(){
$('.profile').append( $(this) );
// Your other custom code
});
}