Création des Logiciels de gestion d'Entreprise, Création et référencement des sites web, Réseaux et Maintenance, Conception
Création des Logiciels de gestion d'Entreprise, Création et référencement des sites web, Réseaux et Maintenance, Conception
JavaScript libraries let developers do more with less code. But JavaScript libraries need to work on a variety of browsers, so using them often means shipping even more code. If JQuery has code to support XMLHttpRequest over ActiveX on an older browser like IE6 then you end up shipping that code even if your application doesn't support IE6. Not only that, but you ship that code to the other 90% of newer browsers that don't need it.
This problem is only going to get worse. Browsers are rushing to implement HTML5 and EcmaScript5 features like JSON.parse that used to be provided only in library code, but libraries will likely have to keep that code for years if not decades to support older browsers.
Lots of compilers (incl. (JSMin, Dojo, YUI, Closure, Caja) remove unnecessary code from JavaScript to make the code you ship smaller. They seem like a natural place to address this problems. Optimization is just taking into account the context that code is going to run in to improve it; giving compilers information about browsers will help them avoid shipping code to support marginal browsers to modern browsers.
The JavaScript Knowledge Base (JSKB) on browserscope.org seeks to systematically capture this information in a way that compilers can use.
It collects facts about browsers using JavaScript snippet. The JavaScript code (!!window.JSON && typeof window.JSON.stringify === 'function') is true if JSON is defined. JSKB knows that this is true for Firefox 3.5 but not Netscape 2.0.
Caja Web Tools includes a code optimizer that uses these facts. If it sees code like
if (typeof JSON.stringify !== 'function') { /* lots of code */ }
it knows that the body will never be executed on Firefox 3.5, and can optimize it out. The key here is that the developer writes feature tests, not version tests, and as browsers roll out new features, JSKB captures that information, letting compilers produce smaller code for that browser.
The Caja team just released Caja Web Tools, which already uses JSKB to optimize code. We hope that other JavaScript compilers will adopt these techniques. If you're working on a JavaScript optimizer, take a look at our JSON APIs to get an idea of what the JSKB contains.
If you're writing JavaScript library code or application code then the JSKB documentation can suggest good feature tests. And the examples in the Caja Web Tools testbed are good starting places.
var divs = document.getElementsByTagName("div");
for (var i=0; i < divs.length; i++) {
var div = document.createElement("div");
document.body.appendChild(div);
}
drawImage()
method allows us to draw only a certain part of a tileset or animation sheet to the screen. In Impact, however, you don't have to deal with any of the Canvas API methods directly. Instead, you specify your tilemaps and animation sheets and let the engine handle the details.// Each animation frame is 16x16 pixelsSimilarly, here's the code needed to create and draw a background layer:
var sheet = new ig.AnimationSheet( 'player.png', 16, 16 );
// This creates the "run" animation: it has 6 frames (the 2nd row
// in the image), with each one being shown for 0.07 seconds
var run = new ig.Animation( sheet, 0.07, [6,7,8,9,10,11] );
// Create a 2D tilemapYou don't have to create these tilemaps by hand. Impact comes with a powerful level editor called Weltmeister, which makes it easy to draw background layers and position your entities (non-static objects in the game world) in your levels.
var map = [
[5, 3, 4],
[2, 7, 1],
[6, 0, 3]
];
// Specify a layer with a tilesize of 16px and our tilemap
var layer = new ig.BackgroundMap( 16, map, 'tileset.png' );
layer.draw();
localStorage
:<script type="text/javascript">function doLogin(doneUrl) {/* Tell XAuth.org that a user has just signed into Google on this browser. */XAuth.extend({// reveals "someone is logged into Google"token: "1",// Expires after 24 hours or if the user explicitly logs outexpire: new Date().getTime() + 60*60*24*1000,// Allow any domain to read this info (could also be a whitelist of partners)extend: ["*"],// Optional callback function once extend() has completed.callback: makeRedirectFunc(doneUrl)});}</script>
<script type="text/javascript">XAuth.retrieve({retrieve: ['xauth.org', 'googxauthdemo.appspot.com', 'xauthdemo.mslivelabs.com'],callback: receiveTokens });</script>
<script type="text/javascript">function receiveTokens(responseObj) {var tokens = responseObj.tokens;var token = tokens['xauth.org'];var partners = {};var tokensFound = false;if (tokens['googxauthdemo.appspot.com']) {partners['google'] = true;tokensFound = true;var status = document.getElementById('status-google');status.innerHTML = 'Signed In!';status.style.color = '#0A0';}}</script>
<div>
, can be marked as editable by adding the contenteditable
attribute:<div contenteditable="true"> ...some content... </div>
You can then manipulate the content with simple calls:document.execCommand("formatting command", showUI, parameter)
For example, you can set the color of the selected text to red with this call:document.execCommand("forecolor", false, "red")
Although this command works well on most browsers, different browsers implement different subsets of formatting commands, and even common commands often produce varying HTML structures.In JavaScript, all three of these statements return true. Doug highlights other traps that are easy to fall into using for..in, ++, and typeof.'' == 0
0 == '0'
'' != '0'
block {
" all on the same line) is the only acceptable style in JavaScript.Marcin |
Aaron |