Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 2 of 3 FirstFirst 123 LastLast
Results 16 to 30 of 34
  1. #16
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Quote Originally Posted by Philip M View Post
    Oh sorry! Mea culpa!

    What is wrong with the <!DOCTYPE>?

    Or do you mean to prefer

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    I prefer..

    Code:
    <!DOCTYPE HTML>
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  2. #17
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    But that's a different doctype than what Philipp had. Actually, using the W3C validator, we can see that all three versions are (or might) be interpreted as different doctypes.

  3. #18
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Quote Originally Posted by Airblader View Post
    But that's a different doctype than what Philipp had. Actually, using the W3C validator, we can see that all three versions are (or might) be interpreted as different doctypes.
    <!DOCTYPE HTML>

    is the declaration for an HTML5 doc-type

    <!DOCTYPE>

    is incomplete.
    Attached Thumbnails Attached Thumbnails Javascript: Writing a function to accept two integer parameters-doctype1.png  
    Last edited by AndrewGSW; 04-13-2013 at 06:15 PM.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  4. #19
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,184
    Thanks
    10
    Thanked 569 Times in 550 Posts
    Quote Originally Posted by AndrewGSW View Post
    I prefer..

    Code:
    <!DOCTYPE HTML>
    yes, this is the minimal text that was discovered to trigger "standards compliance mode" in all known existing browsers, including IE6.
    since that's the point of a doctype as far as the browser is concerned, they made that trigger-text the doctype for HTML5.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5

  5. #20
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by rnd me View Post
    yes, this is the minimal text that was discovered to trigger "standards compliance mode" in all known existing browsers, including IE6.
    since that's the point of a doctype as far as the browser is concerned, they made that trigger-text the doctype for HTML5.
    Is not <!DOCTYPE><html> not the same as <!DOCTYPE html>?

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  6. #21
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,184
    Thanks
    10
    Thanked 569 Times in 550 Posts
    Quote Originally Posted by Philip M View Post
    Is not <!DOCTYPE><html> not the same as <!DOCTYPE html>?
    no, it's not.
    if you look at the old doctypes, they always go "<!DOCTYPE HTML " SCOPE VERSION URL.

    some doctypes don't use HTML as the system ID.
    for example,

    mathml:
    Code:
    <!DOCTYPE math PUBLIC "-//W3C//DTD MathML 2.0//EN"	
    	"http://www.w3.org/Math/DTD/mathml2/mathml2.dtd">
    or svg:
    Code:
    <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
    	"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">

    after the " HTML", some browsers do their own minor sub-feature configuring, but standards-compliance render mode is enabled no matter what comes next, so html5 just stops while it's ahead...
    Last edited by rnd me; 04-13-2013 at 07:19 PM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5

  7. #22
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,455
    Thanks
    0
    Thanked 632 Times in 622 Posts
    The modern way which also supports IE4:

    Code:
    if (!Object.keys) {
      Object.keys = (function () {
        var hasOwnProperty,hasDontEnumBug, dontEnums, dontEnumsLength;
        hasOwnProperty = Object.prototype.hasOwnProperty;
        hasDontEnumBug = !({toString: null}).propertyIsEnumerable('toString');
        dontEnums = ['toString','toLocaleString','valueOf','hasOwnProperty','isPrototypeOf','propertyIsEnumerable','constructor'];
        dontEnumsLength = dontEnums.length;
        return function (obj) {
          var result, prop, i; 
          if (typeof obj !== 'object' && typeof obj !== 'function' || obj === null) throw new TypeError('Object.keys called on non-object');
          result = [];
          for (prop in obj) {
            if (hasOwnProperty.call(obj, prop)) result.push(prop);
          }
          if (hasDontEnumBug) {
            for (i=0; i < dontEnumsLength; i++) {
              if (hasOwnProperty.call(obj, dontEnums[i])) result.push(dontEnums[i]);
            }
          }
          return result;
        }
      })()
    };
    
    getNumsBetween = function(a, b) {
      if (a !== Math.floor(+a) || b !== Math.floor(+b)) throw new TypeError();
      if (a < 0 || b < a) throw new RangeError();
      return Object.keys(new String(Array(b+2))).slice(a).join(' ');
    }
    
    alert(getNumsBetween(6,10)); // '6 7 8 9 10'
    alert(getNumsBetween(7,-10)); // range error
    alert(getNumsBetween('a',27)); // type error
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  8. #23
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by felgall View Post
    The modern way which also supports IE4:
    Yep. Longer and more complicated (and must be slower, as if anyone cared) that the code I gave which works in any browser. A good example of not using 10 words where 50 will do.

    To be a successful teacher you must be more interested in the students than in the subject. One more time - it is quite wrong to suppose that someone who is highly knowledgable and competent in his subject can ipso facto teach it.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  9. #24
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Perhaps I'm missing something, but I don't see the point in throwing a TypeError, or RangeError? There is nothing to catch this error so all it will do is to halt the script.

    Well, perhaps that is the intention.. Why not just return undefined.
    Last edited by AndrewGSW; 04-14-2013 at 01:38 PM.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  10. #25
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,184
    Thanks
    10
    Thanked 569 Times in 550 Posts
    Quote Originally Posted by AndrewGSW View Post
    Perhaps I'm missing something, but I don't see the point in throwing a TypeError, or RangeError? There is nothing to catch this error so all it will do is to halt the script.

    Well, perhaps that is the intention.. Why not just return undefined.
    i would imagine the throw is to make it quack like a native method.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5

  11. #26
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Quote Originally Posted by rnd me View Post
    i would imagine the throw is to make it quack like a native method.
    Ta
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  12. #27
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,455
    Thanks
    0
    Thanked 632 Times in 622 Posts
    Quote Originally Posted by AndrewGSW View Post
    Perhaps I'm missing something, but I don't see the point in throwing a TypeError, or RangeError? There is nothing to catch this error so all it will do is to halt the script.

    Well, perhaps that is the intention..
    Exactly.

    The only reason for those statements is with a shared function where you don't have control of who is going to call it. You want it to crash immediately with an appropriate error if someone tries to call it with a wrong parameter so they can fix their source code. Those errors should never occur once the code goes live as the calling code should have made sure that the numbers are valid before making the call.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  13. #28
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,455
    Thanks
    0
    Thanked 632 Times in 622 Posts
    Quote Originally Posted by Philip M View Post
    Yep. Longer and more complicated (and must be slower, as if anyone cared) that the code I gave which works in any browser. A good example of not using 10 words where 50 will do.
    Possibly slower in IE8 and earlier browsers - which are the only ones that actually run any of the extra code. Also all that extra code would be a part of a library adding modern JavaScript support to modern browsers and would probably be added in a conditional comment that only loads it for IE8 and earlier.

    All the current browsers would only need the much shorter modern code which would run much faster because it doesn't contain a loop in the JavaScript.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  14. #29
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    Quote Originally Posted by AndrewGSW View Post
    Perhaps I'm missing something, but I don't see the point in throwing a TypeError, or RangeError?
    If your function was called incorrectly, don't return anything throw an error to force the caller to either feed correct values or implement proper error handling for runtime problems.

    Even returning undefined is still returning something, which signals that everything was fine, but maybe the function just couldn't find anything or whatever. It covers the fact that there was an incorrect call, and only makes debugging harder. With an uncaught error or a proper error handling, it will immediately be visible what went wrong or at least where it went wrong. But an undefined could be passed through many functions before it leads to a problem (wrong result or some other method finally throwing an error).

  15. #30
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Quote Originally Posted by felgall View Post
    Possibly slower in IE8 and earlier browsers - which are the only ones that actually run any of the extra code. Also all that extra code would be a part of a library adding modern JavaScript support to modern browsers and would probably be added in a conditional comment that only loads it for IE8 and earlier.
    It doesn't really require an IE8 conditional though, does it? Because it starts with:

    Code:
    if (!Object.keys) {
    MDN reference
    BTW What are these fill-in-snippets called? "fill-ins"? "poly-fills"? I seem to recall there was a (semi-formal) term for them.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS


 
Page 2 of 3 FirstFirst 123 LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •