only.js
Version:
Automatically load and execute selector-based dependencies
101 lines (85 loc) • 3.35 kB
HTML
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Only.js tests (Only.js with conditional script executing)</title>
<script src="../src/Only.js"></script>
<!-- testing conditional script executing -->
<script>
var timeDiff = function(){
var d, time;
return {
setStartTime:function (){
d = new Date();
time = d.getTime();
},
getDiff:function (){
d = new Date();
return (d.getTime()-time);
}
};
}();
function done() {
var log_value_text = "Loading/Execution Time: "+timeDiff.getDiff()+"\n";
(function fn(){
var load_log = document.getElementById("load_log");
if (load_log !== null) load_log.value += log_value_text;
else setTimeout(fn,10);
})();
}
timeDiff.setStartTime();
$O.setDefaults({
autoPolyfill: false
});
// test with separate object chains
$O.ready(function () {
$O
// test with in-existent selector first
.test('.form')
.test('textarea')
.wait(function () {
console.warn('should not run'); // should not run, '.form' does not exist
done();
});
// test with in-existent selector last
$O
.test('form')
.test('.textarea')
.wait(function () {
console.warn('should not run'); // should not run, '.textarea' does not exist
done();
});
// test with 'and'-like selector dependency
$O
.test('form')
.test('textarea')
.wait(function () {
console.warn('should run : and-like'); // should run, selectors exist
var load_log = document.getElementById("load_log");
if (load_log !== null) load_log.value += "test with 'and'-like selector dependency successful!\n";
done();
});
// test with 'or'-like selector dependency
$O
.test('form, textarea')
.wait(function () {
console.warn('should run : or-like'); // should run, selectors exist
var load_log = document.getElementById("load_log");
if (load_log !== null) load_log.value += "test with 'or'-like selector dependency successful!\n";
done();
});
});
</script>
</head>
<body>
<h1>Only.js tests (Only.js with conditional script executing)</h1>
<img src="img1.jpg" width="100" hspace="5" alt="image 1" title="size: 379kb" />
<img src="img2.jpg" width="100" hspace="5" alt="image 2" title="size: 20kb" />
<br />
<form name="log_form">
<label>
<textarea id="load_log" name="load_log" cols="80" rows="10"></textarea>
</label>
</form>
</body>
</html>