Skip to content

Instantly share code, notes, and snippets.

Avatar
🎯
Focusing

Ryan Wu ryanhanwu

🎯
Focusing
View GitHub Profile
@ryanhanwu
ryanhanwu / docwrite.js
Created Apr 13, 2012
Do not use document write to create script tag
View docwrite.js
//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);
View cool.js
//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 Apr 20, 2012 — forked from bmeck/__stack.js
stack trace api fun for the masses.
View __stack.js
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 Apr 27, 2012
JavaScript Scope Chain - Variable
View run.js
//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 Jun 4, 2012
JavaScript Anti-Pattern
View gist:2866961
* 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 Jun 26, 2012
iOS note for cocos2d alert and basic concept
View ios_note_0626
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 Jun 28, 2012
Cell comparasion
View gist:3009037
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 Aug 6, 2012 — forked from xonecas/wget
simple wget for node.js
View wget.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 = '';
View preloadimg.js
$(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 Aug 28, 2012
image preloader (do after all loaded)
View preloaderImg_simple.ks
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
});
}