Top Banner
JAVASCRIPT NÃO-OBSTRUTIVO com jQuery Por Marcelo Fraga
17

JAVASCRIPT NÃO-OBSTRUTIVO com jQuery

Jul 12, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: JAVASCRIPT NÃO-OBSTRUTIVO com jQuery

JAVASCRIPTNÃO-OBSTRUTIVO

com jQuery

Por Marcelo Fraga

Page 2: JAVASCRIPT NÃO-OBSTRUTIVO com jQuery

O QUE É?Um conjunto de princípios para uma escrita de

JavaScript acessível, de fácil manutenção.

Page 3: JAVASCRIPT NÃO-OBSTRUTIVO com jQuery

CARACTERÍSTICAS• Sempre aplicar JavaScript em arquivos externos.

• Nunca utilizar JavaScript diretamente no documento.

• Usar JavaScript como incremento, não como uma funcionalidade segura.

• A página continua sendo utilizável, mesmo sem o suporte a JavaScript.

Page 4: JAVASCRIPT NÃO-OBSTRUTIVO com jQuery

EXEMPLOUma vez que a página foi carregada, o JavaScript:

• Encontra todos os labels ligados a um campo de texto.

• Move o texto do label para o campo de texto associado.

• Esconde os labels.

• Estabelece eventos para remover o texto descritivo quando o campo está em foco.

Page 5: JAVASCRIPT NÃO-OBSTRUTIVO com jQuery

EXEMPLO

Page 6: JAVASCRIPT NÃO-OBSTRUTIVO com jQuery

O JAVASCRIPT$('label').each(function() { var $label = $(this), $input = $('#' + $label.attr('for')), initial = $label.hide().text(); $input.focus(function() { if ($input.val() == initial) { $input.val(‘’).css('color', '#000'); } }).blur(function() { if (!$input.val()) { $input.val(initial).css('color', '#aaa'); } }).css('color', '#aaa').val(initial);});

Page 7: JAVASCRIPT NÃO-OBSTRUTIVO com jQuery

O HTML

<form action=”procurar.php”><label for=”procurar”>Procurar</label><input type=”text” id=”procurar” name=”procurar” /><button type=”submit”>ok</button>

</form>

Page 8: JAVASCRIPT NÃO-OBSTRUTIVO com jQuery

MAL USO

<a href=”javascript: window.open(‘http://zigotto.com.br’);”>Zigotto</a>

Page 9: JAVASCRIPT NÃO-OBSTRUTIVO com jQuery

MAL USO

<a href=”#” onclick=”javascript: window.open(‘http://zigotto.com.br’); return false;”>Zigotto</a>

Page 10: JAVASCRIPT NÃO-OBSTRUTIVO com jQuery

UM POUCO MELHOR

<a href=”http://zigotto.com.br” onclick=”javascript: window.open(this.href); return false;”>Zigotto</a>

Page 11: JAVASCRIPT NÃO-OBSTRUTIVO com jQuery

O MELHOR

$('a[rel="externo"]').click(function(e) {window.open(this.href);e.preventDefault();

});

<a href=”http://zigotto.com.br” rel=”externo”>Zigotto</a>

Page 12: JAVASCRIPT NÃO-OBSTRUTIVO com jQuery

A IMPLEMENTAÇÃOGARBER-IRISH

<body data-controller=”<%= params[:controller] %>” data-action=”<%= params[:action] %>”>

Page 13: JAVASCRIPT NÃO-OBSTRUTIVO com jQuery

A IMPLEMENTAÇÃOGARBER-IRISH

<body id=”cart” class=”shopping”>

Page 14: JAVASCRIPT NÃO-OBSTRUTIVO com jQuery

A IMPLEMENTAÇÃOGARBER-IRISH

SITENAME = {common: {

init: function() {// code

}},users: {

init: function() {// code

},show: function() {

// code}

}};

Page 15: JAVASCRIPT NÃO-OBSTRUTIVO com jQuery

A IMPLEMENTAÇÃOGARBER-IRISH

UTIL = {exec: function(controller, action) {

var namespace = SITENAME, action = (action === undefined) ? “init” : action;

if (controller && namespace[controller] && typeof(namespace[controller][action] == “function”)) {namespace[controller][action]();

}},init: function() {

var $body = $(document.body), controller = $body.data(‘controller’), action = $body.data(‘action’);

UTIL.exec(‘common’);UTIL.exec(controller);UTL.exec(controller, action);

}};

Page 16: JAVASCRIPT NÃO-OBSTRUTIVO com jQuery

A IMPLEMENTAÇÃOGARBER-IRISH

$(document).ready(UTIL.init);

Page 17: JAVASCRIPT NÃO-OBSTRUTIVO com jQuery

FONTES• http://dev.opera.com/articles/view/the-seven-rules-of-unobtrusive-javascrip/

• http://paulirish.com/2009/markup-based-unobtrusive-comprehensive-dom-ready-execution/

• http://www.viget.com/inspire/extending-paul-irishs-comprehensive-dom-ready-execution