Jump to content
Larry Ullman's Book Forums

QuakeLive

Members
  • Posts

    50
  • Joined

  • Last visited

  • Days Won

    1

Posts posted by QuakeLive

  1. Quote [page 413 - Character Classes... ]:

     

     

    3. Check if a string contains no spaces. The \S character class shortcut will match non-white space characters. To make sure that the entire string contains no spaces, use the caret and the dollar sign: ^\S$. If you don’t use those, then all the pattern is confirming is that the subject contains at least one non-space character.

     

    But, ^\S$ does not check if a string contains no spaces, it only checks if a string is a non-white space character, right?

     

    2rrw41z.jpg

  2. Hi,

     

    I tested the example " Using the Cookie Library"... also I saw this in errata :

     

    2e3o16p.png

     

    and everything works fine, but I think that now the cookie is not renewed since the setTheme(theme) function does not create (set) any cookie. And, in the book there is the following text:

     

    However, as written, the theme cookie will expire in a week. By calling the setThemeCookie() function here, the cookie will be renewed for another week (from today), and the page will be updated. This keeps the user’s preferences retained so long as it hasn’t been more than a week since the user accessed the page.

     

    But, in eratta it is not explained how to renew the cookie. What would be the best and most elegant way to do this? This is how I did:

    window.onload = function() {
    	'use strict';
    	document.getElementById('aTheme').onclick = setThemeCookie;
    	document.getElementById('bTheme').onclick = setThemeCookie;
    	var value = COOKIE.getCookie('theme');
    	if (value) {
    		setTheme(value);
                    var expire = new Date();
    		expire.setDate(expire.getDate() + 7);
    		COOKIE.setCookie('theme', value, expire);
    	}
    };
    

    The original code was:

    window.onload = function() {
            'use strict';
            document.getElementById('aTheme').onclick = setThemeCookie;
            document.getElementById('bTheme').onclick = setThemeCookie;
            var theme = COOKIE.getCookie('theme');
            if (theme) {
                    setTheme(theme);
            }
    };
    

    2. I have another question:

    • the cookie was created (for example, I clicked on the link 'aTheme');
    • then I browsed to some other website (for example, I opened google.com);
    • and, finnaly, I oppened the same example (page) again (by entering path in a browser).

    Why theme is not remembered, CSS is not loaded? I need to click, to choose theme again to load CSS on the page? This applies to both - the code downloaded from this website, and for my code.

  3. At the beginning, I thought that I didn't understand some things about cookies well and that's why I asked:

     

    Is it correct to say that "some browsers add a space in between the cookies in document.cookie" INSTEAD OF "some browsers add a space in between the cookie values in document.cookie" ???

     

    After that, I just gave my opinion, "splitting semantic-hairs" (or some kind of provocation by trying to find someone else's mistake) was not my intention.

     

    Anyway, now it is clear, thank you very much HartleySan and Larry!

  4. I dunno. I think those are rather minor differences. But if you like it your way, that's fine, too.

    Thanks. I think that using "Cookies" instead of "Cookie values" is more correct (in the quotes above), because "cookie value" is just a part of the cookie (name-value, duration... )... but not sure, and it does not seem so important... Anyway, thank you again!

  5. Hi,

     

    Here are two quotes from Creating Cookie Library example:

     

     

    Begin looping through the cookies:

     

    for (var i = 0, count = cookies.length; i < count; i++) {
                    var value = (cookies.slice(0,1) == ‘ ‘) ? cookies.slice(1) : cookies;

     

    ... some browsers add a space in between the cookie values in document.cookie.

     

    Decode the value:

     

    value = decodeURIComponent(value);

     

     

    Would not it be better if instead of "cookie values" we use just "cookies" (... some browsers add a space in between the cookies in document.cookie. )

     

    and

     

    "Decode the cookie" instead of "Decode the value"?

     

    Is it correct to say that "some browsers add a space in between the cookies in document.cookie" INSTEAD OF "some browsers add a space in between the cookie values in document.cookie" ???

     

    Thank you in advance!

     

  6. Hi,

     

    I have a question about Creating Modal Windows example... Quote:

     

     

    function openModal() {
        'use strict';
        document.getElementById('closeModal').onclick = closeModal;
        document.getElementById('modal').style.display = 'inline-block';
        document.getElementById('openModal').onclick = null;
    }

    function closeModal() {
        'use strict';
        document.getElementById('openModal').onclick = openModal;
        document.getElementById('modal').style.display = 'none';
        document.getElementById('closeModal').onclick = null;
    }

    window.onload = function() {
        'use strict';
        document.getElementById('openModal').onclick = openModal;
    };

     

    If I remove the following lines (altogether, all three lines):

    • In function openModal(): document.getElementById('openModal').onclick = null;
    • In function closeModal(): document.getElementById('openModal').onclick = openModal;
    • In function closeModal(): document.getElementById('closeModal').onclick = null;

    so that the new code looks like this:

     

     

    function openModal() {
        'use strict';
        document.getElementById('closeModal').onclick = closeModal;
        document.getElementById('modal').style.display = 'inline-block';
    }

    function closeModal() {
        'use strict';
        document.getElementById('modal').style.display = 'none';
    }

    window.onload = function() {
        'use strict';
        document.getElementById('openModal').onclick = openModal;
    };

     

    everything works well, there are no errors. I don't understand why there are these three lines?

     

    Thank you in advance!

  7. Sorry for the late reply - thank you very much HartleySan!

     

    And what about attributes? For now, in the book I did not find anything, only that are treated as nodes... (also, I found something here). But, if all HTML attributes are attribute nodes - why in the previous example (with the number of children of paragraph) we didn't count attributes nodes? Or, there is an exception in the case of attributes nodes?

  8. To answer your questions:

     

    1) The html element is the root node of an HTML document, but not the root element of the DOM, which is a different thing. The DOM is an API that allows you to search through and manipulate the structure of the current HTML document. The DOM requires an object above the root node so that you can access the root node (the html element) in the actual HTML document.

     

    2) Sounds to me like the information in Larry's book is wrong. I would trust the info you see on all the other sites since they are all consistent and reliable. Also, because Larry's statement "10 for the HTML element" is incorrect, I wouldn't worry about the meaning, but basically, 1 is a standard HTML element (e.g., div, img, a, li, etc.), and 10 is the doctype (or DTD, for short) of the HTML document.

     

    That make sense?

     

    Yes, thank you very much HartleySan.  So, the object above the root node is document object.

     

    Let's look at this paragraph:

    <p>This is some text and if you click <a href="#">here</a>, something will happen.<br> This is another text.</p>

    1. This paragraph has has three children (text node, A node and BR node), right?

     

    2. The whole text is treated as one child, although there is some text before <a></a> element, some text is between <a></a> and <br> elements, and some text is after <br> element?

     

    3. The only difference between childNodes property and children property is that children property excludes the TEXT nodes? And that's all? :)

  9. Hi,

     

    I started reading the chapter about Document Object Model, and I have a few beginner questions:

     

    1. Quote from book:

     

    The root node, document in a Web page, has no parent and only one child: html.

     

    Isn't HTML element the root node?

     

    pic_htmltree.gif

     

     

     

    2. Quote from book:

     

    The nodeType property will be a number:

    • 1 for an HTML element
    • 2 for text
    • 8 for comments
    • 9 for the document
    • 10 for the HTML element

     

    But on http://www.w3schools.com/jsref/prop_node_nodetype.asp and https://developer.mozilla.org/en-US/docs/Web/API/Node.nodeType this is different:

     

    24vl0ug.png

     

    Also, what is the difference between "1 for an HTML element" and  "10 for the HTML element"? I guess that's a mistake.

     

    Thank you in advance!

  10. "It" (I'm assuming the callback function) knows that e represents the Event object because that's the way JavaScript is designed and implemented in all browsers.

     

    Basically, whenever you have a parameter defined in a callback function header, that argument (or more specifically, the first argument) is always assigned the Event object, regardless of whether you use the variable e or some other variable name (I usually use the variable name evt).

     

    If you were to call the callback function manually (i.e., not by making an event occur), then the first argument you pass the function would be assigned to e instead of the Event object. This is kind of an edge case though, as callback functions are rarely used as regular functions that are called without the occurrence of an event.

     

    Does that all make sense?

     

    Yes, thank you very much HartleySan, now it is clear! :)

  11. Hi,

     

     

    Event handlers automatically receive a single argument, which represents the event that occurred... You can write your handlers to accept this argument:

     

              

              function someEventHandler(e) {

               // Use e.

              }

     

    I do not understand how does "it" know that argument e represents event that occured, not something else. What if that is some other argument, not representing event?

     

    Sorry for my bad English.

     

    Thank you in advance!

  12. If the return true line is not in the code, then the function will always return undefined no matter what.

    Whenever a return statement is encountered, the function is immediately exited, and nothing after the return statement is executed.

    However, if there is no return statement in a function, then when the end of the function is reached, JavaScript implicitly returns undefined from the function.

     

    In other words, without return true, you have no way of knowing whether the first if statement was satisfied or not because undefined would always be returned.

    Yes, my mistake, I need to check (test) before I ask... ^_^

     

    23r1hy0.png

     

    Thank you very much!

  13. Note that true is returned only when the first if statement in the function is satisfied.

    If that if statement is not satisfied, then true is not returned, and implicitly, at the end of the function, JS will return undefined (which loosely evaluates to false).

    As such, you can check the return value to know whether or not the first if statement was satisfied or not.

    Thank you HartleySan. If I understood correctly - that is just good practice (because of debugging)... But let's say that there is no that line: return true;. If the first statement in the function is satisfied - JS function will not return undefined (which loosely evaluates to false), right? If so, then why return true?

  14. Hi,

     

    I have one simple question about creating a utility library:

     

     

    var U = {
        $: function(id) {
            'use strict';
            if (typeof id == 'string') {
                return document.getElementById(id);
            }
        }
        setText: function(id, message) {
            'use strict';
            if ((typeof id == 'string') && (typeof message == 'string')) {
                var output = this.$(id);
                if (!output) return false;
                if (output.textContent !== undefined) {
                    output.textContent = message;
                } else {
                    output.innerText = message;
                }
                return true;
            }
        }

    .

    .

    .

    .

     

    Is it necessary to return true, and why this method must return true ?

     

    Thank you in advance :)

     

     

  15. Hmmm... you're right. That's interesting.

    My guess would be that browser makers realized the danger of allowing undefined to be redefined, and block any attempts to do so.

     

    If that's the case, then with newer browsers, you no longer have to worry about this issue (although it'll still be an issue in older browsers (i.e., old IE)).

     

    I found this... where someone wrote that "The ES5 standard dictates that undefined is now readonly." :)

    • Upvote 1
  16. HartleySan, THANK YOU SO MUCH!

     

    I still haven't started reading part of the book that relates to event handling, the last chapter that I read is Returning Values From Functions...

     

    But your post explains the difference between

     

    object.property = functionReference;

    and

    object.property = functionCall();

     

    If I understood well, this is the point:

     

    - assigning a function call to the event handler actually causes the function assigned to the handler to be called immediately, and then assign the value returned by the function call to the event handler.

     

    - all a function reference is is something that says, "Call function xyz when the event occurs."

     

     

    Actually, as it is written in the book - with function reference a function definition can be a value assigned to a variable (object property)...

     

    All this is well explained in the book.

     

    The reason I got confused and I did not pay enough attention is because of the following example:

     

    t8nhbq.jpg

    (this is from book)

     

    When I tried to test - function definition was not returned when called function reference (don't know why?):

     

    2lasn75.png

    (only "function()" is returned)

     

     

     

    Anyway, the point is that with function reference a function definition will be returned (and assigned... ), and with function call - the value returned by the function will be assigned...

     

    THANK YOU AGAIN, HartleySan!

    • Upvote 1
  17. Ok, as I read the book, some things have changed, but there is still a problem.

     

    As for the example in my previous post, if there is ONLY

     

    function init() {

    //some code

    return something;

    }

     

    that does not mean that window.onload = init; would be wrong and cause some error, because for every function we can use identifier which is different from a function call. 

    But, now, I do not understand what is the difference between identifier and a function call. I know that when you say:

     

    window.onload = init();

     

    you actually called the init() function, and that's ok, BUT I do not understand what happens when you use  function identifier:

     

    window.onload = init;

     

    if with this line I did not call a function which will do something,  then what I actually did??? :unsure:

  18.  

    This is code that’s been used many times over in this book:

     

    window.onload = init;

     

    That code assigns to the onload property of the window object the value of the init variable, which is to say the init() function definition.

     

    Note that the code does not invoke the function—it’s lacking the invocation parentheses:

     

    window.onload = init(); // No!

     

    Doing the above would call the init() function and assign the value returned by it to the window.onload property, which is not the intent.

     

     

    Hi,

     

    I have one question about this. In the book it is written that "Doing the above would call the init() function and assign the value returned by it to the window.onload property, which is not the intent."

     

     

     

     

    Let's say that somewhere in JavaScript exsist the following code:

     

    var init = function() {

    //some code

    return something;

    }

     

    Here, a function definition is a value assigned to a variable. Now, we have:

     

    window.onload = init;

     

    and that code assigns to the onload property of the window object the value of the init variable.

     

    OK. Let's suppose that there exists the following function which does the same

     

    function init() {

    //some code

    return something;

    }

     

    In this case, it would be the same to use:

     

    window.onload = init;

    AND

    window.onload = init();

     

    But in reality, this is not the case, there is no init() function, there is only init variable so it isn't the same. Am I right?

  19. Thank you HartleySan!

     

    Although I read this book, I do not think I understood some things well. For example:

     

     

    You can change the value of undefined:

    undefined = 42;

     

    So, now undefined is number?

     

    And, if I understood  well - it is better to use everywhere (typeof something != 'undefined'), for example:

     

                                                    if (typeof output.textContent != 'undefined') {

                                                                    output.textContent = message;

                                                    } else {

                                                                    output.innerText = message;

                                                    }

     

    (than

                                                    if (output.textContent !== undefined) {

                                                                    output.textContent = message;

                                                    } else {

                                                                    output.innerText = message;

                                                    }

    as used in the book?)

×
×
  • Create New...