Global variables
. , . , , .
myNamespace:
var myNamespace = window.myNamespace || {};
myNamespace.variable = 'Some value';
, JSON HTML script.
, PHP, , PHP json_encode:
<?php
$book = array(
"title" => "Eloquent JavaScript",
"author" => "Marijn Haverbeke",
"edition" => 2
);
?>
<script>
var myNamespace = '<?php echo json_encode($book, JSON_PRETTY_PRINT) ?>';
/* var myNamespace = {
"title": "Eloquent JavaScript",
"author": "Marijn Haverbeke",
"edition": 2
};
*/
</script>
JSON_PRETTY_PRINT , JSON , , , , . , , JavaScript .
. myNamespace , IIFE, . window.myNamespace, , , myNamespace. AJAX .
var myNamespace = (function(namespace) {
namespace.a = (function() { return ... })();
namespace.b = (function() { return ... })();
return namespace;
})(window.myNamespace || {});
AJAX .
DOM
DOM - HTML, .
<img src="path/to/image.jpg" id="myId" class="myClass" title="My title" data-owner="Joe Bloggs" data-year="2017" />
DOM , . , , document.getElement... DOM.
var attrs = document.getElementById('unique-id').attributes;
Array.prototype.slice.call , :
Array.prototype.slice.call(document.getElementById('myId').attributes)
.forEach(function(item) {
console.log(item.name + ': '+ item.value);
});
LocalStorage
localStorage - / , , . " " - , .
cross-storage Zendesk .
> DOM > LocalStorage > AJAX
- .
- DOM , .
- LocalStorage / .
- AJAX , .