Skip to content

Instantly share code, notes, and snippets.

@adamsilver
Created May 29, 2012 07:00
Show Gist options
  • Save adamsilver/2823034 to your computer and use it in GitHub Desktop.
Save adamsilver/2823034 to your computer and use it in GitHub Desktop.
Jessie and conditional comment proposal design problem
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Title</title>
</head>
<body>
<script src="jessie.js"></script>
<!--[if lte IE 8]><script src="jessie-ie8-and-below.js"></script><![endif]-->
</body>
</html>
(function(global) {
var globalDocument = global.document,
isHostObjectProperty = function(object, property) {
return !!(typeof(object[property]) == 'object' && object[property]);
},
isHostMethod = function(object, method) {
var type = typeof object[method];
return type == 'function' ||
type == 'object' && !!object[method] ||
type == 'unknown';
},
html = isHostObjectProperty(globalDocument, 'documentElement') && globalDocument.documentElement,
canCall = !!Function.prototype.call;
/*
Description:
Relies on Microsoft's `el.attachEvent` implementation for IE8-
*/
/*
Support:
IE6, IE7
*/
var attachListener;
attachListener = function(el, eventType, fn) {
var listener = function() {
var e = window.event;
fn.call(e, e);
};
el.attachEvent('on'+eventType, listener);
// Discard unneeded references
// Prevents circular references with host objects (e.g. the element)
// Prevents IE leaks related to such circular references
// No unload event cleanup required
el = null;
return listener;
};
global.jessie = {
isHostMethod: isHostMethod,
isHostObjectProperty: isHostObjectProperty,
attachListener: attachListener
}
globalDocument = html = null;
})(this);
(function(global) {
var globalDocument = global.document,
isHostObjectProperty = function(object, property) {
return !!(typeof(object[property]) == 'object' && object[property]);
},
isHostMethod = function(object, method) {
var type = typeof object[method];
return type == 'function' ||
type == 'object' && !!object[method] ||
type == 'unknown';
},
html = isHostObjectProperty(globalDocument, 'documentElement') && globalDocument.documentElement,
canCall = !!Function.prototype.call;
/*global canCall */
/*
Description:
Relies on `unction.prototype.bind`
*/
var bind;
if(canCall && Function.prototype.bind){
bind = function(fn, thisObject) {
return fn.bind.apply(fn, Array.prototype.slice.call(arguments, 1));
};
}
/*global bind,attachListener */
/*
Description:
Relies on `jessie.bind` and `jessie.attachListener`
*/
var attachBoundListener;
if(bind && attachListener) {
attachBoundListener = function(el, eventType, fn, thisObject) {
var listener = bind(fn, thisObject);
thisObject = null;
return attachListener(el, eventType, listener);
};
}
global.jessie = {
isHostMethod: isHostMethod,
isHostObjectProperty: isHostObjectProperty,
bind: bind,
attachBoundListener: attachBoundListener
}
globalDocument = html = null;
})(this);
@adamsilver
Copy link
Author

There is a problem here: attachBoundListener is defined before attachListener is defined.

Also each of the individual feature tests i.e. if(bind && attachListener) {....... are checked against locally scoped variables. This means that even if we could (by placing ie8 conditional comments before main file) get ie8-and-below.js to run first they would only be defined and exposed to the world as "jessie.attachListener" and not "attachListener".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment