In the previous chapters, the solutions to the exercises could be found in the text of the chapter. This time, some of the exercises require you to do some more reading, or experimentation, outside this book.
*()
functions, one or more calls to setInterval()
, and maybe one to setTimeout()
/clearInterval()
to stop the whole thing. Or, here's an easier one-print the current date/time in document.title
and update it every second, like a clock.walkDOM()
differently. Also, make it accept a callback function instead of hard coding console.log()
.innerHTML
is easy (document.body.innerHTML = ''
), but not always best. The problem will be when there are event listeners attached to the removed elements; they won't be removed in IE, causing the browser to leak memory because it stores references to something that doesn't exist. Implement a general-purpose function that deletes DOM nodes, but removes any event listeners first. You can loop through the attributes of a node and check if the value is a function. If it is, it's most likely an attribute like onclick
. You need to set it to null
before removing the element from the tree.include()
that includes external scripts on demand. This means you need to create a new <script>
tag dynamically, set its src
attribute, and append to the document's <head>
. Test it by using the following code:> include('somescript.js'),
myevent
, which has the following methods working cross-browser: addListener
(element
, event_name
, callback)
, where element
can also be an array of elementsremoveListener
(element
, event_name
, callback)
getEvent(event)
just to check for a window.event
for older versions of IEfunction myCallback(e) { e = myevent.getEvent(e); alert(myevent.getTarget(e).href); myevent.stopPropagation(e); myevent.preventDefault(e); } myevent.addListener(document.links, 'click', myCallback);
href
attribute.<div>
, say at x = 100px
, y = 100px
. Write the code to be able to move div around the page using the arrow keys or the J (left), K (right), M (down), and I (up) keys. Reuse your own event utility from 3.1.ajax
. For example, take a look at the following code:function myCallback(xhr) { alert(xhr.responseText); } ajax.request('somefile.txt', 'get', myCallback); ajax.request('script.php', 'post', myCallback, 'first=John&last=Smith'),
52.15.63.145