document.getElementById('content').focus();

Discussion in 'Javascript' started by agflem@yahoo.ca, Jan 24, 2005.

  1. Guest

    Why won't this work in Firefox:
    document.getElementById('content').focus();
    Is there something else to use?
    , Jan 24, 2005
    #1
    1. Advertising

  2. RobB Guest

    wrote:
    > Why won't this work in Firefox:
    > document.getElementById('content').focus();
    > Is there something else to use?


    What's the context in which you're using it? Is 'content' an id rather
    than a name? Is this a form element?
    RobB, Jan 25, 2005
    #2
    1. Advertising

  3. RobG Guest

    wrote:
    > Why won't this work in Firefox:
    > document.getElementById('content').focus();
    > Is there something else to use?


    Advice above is good. If you are going to compare date strings, you
    must compose them yourself.

    var dtToday = Date()

    will create a new date object with a date/time of the instant it was
    created according to the settings of the user's local machine. This is
    likely not accurate or reliable, so you must validate on the server.
    Simply writing dtToday will create some string according to the
    settings of the local machine, which could be anything.

    Supposing txtDDate is the string "01/24/2005", then the equivalent date
    string constructed from dtToday will be given by the script below.
    Note that date & month less than "10" need a leading zero and that
    getFullYear() is not supported on all browsers, so you may need to use
    getYear and deal with it's eccentricities:


    <script type="text/javascript">
    function checkZero(a){
    return (a < 10)? "0" + a : a;
    }
    function todayDate() {
    var dtToday = new Date();
    var dtTodayString = checkZero(+dtToday.getMonth()+1)
    + '/' + checkZero(dtToday.getDate())
    + '/' + dtToday.getFullYear();
    alert(dtTodayString);
    }
    </script>
    <button onclick="todayDate()">Click me</button>

    In regard to date formats, if users' aren't going to see them (or
    perhaps even if they are) the accepted international standard is
    ISO8601. Your date above would become 2005-01-24 or 2005/01/24. Read
    stuff on dates here:

    <URL:http://www.merlyn.demon.co.uk/js-dates.htm>

    The format mm/dd/yyyy is peculiar to the US and is likely to be
    misinterpreted elsewhere in the world. ISO8601 dates should not be.

    I appologise for baddly formatted code, but Google Groups insists on
    removing all leading spaces, so there's not much I can do.
    RobG, Jan 25, 2005
    #3
  4. JRS: In article <>,
    dated Mon, 24 Jan 2005 17:45:19, seen in news:comp.lang.javascript, RobG
    <> posted :
    > wrote:
    >> Why won't this work in Firefox:
    >> document.getElementById('content').focus();
    >> Is there something else to use?

    >
    >Advice above is good.


    It does not look like advice; and your response appears unrelated.

    > If you are going to compare date strings, you
    >must compose them yourself.


    That is not a requirement. It is, for example, perfectly correct to
    compare for equality date strings in any fixed format; and to compare
    for order date strings in an ISO 8601 format. User input validation can
    easily ensure comparability.

    >var dtToday = Date()
    >
    >will create a new date object with a date/time of the instant it was
    >created according to the settings of the user's local machine.


    For me, it creates a string, according to typeof .

    > This is
    >likely not accurate or reliable, so you must validate on the server.
    >Simply writing dtToday will create some string according to the
    >settings of the local machine, which could be anything.


    In my system, it is unambiguous; its value may be incorrect.

    >Supposing txtDDate is the string "01/24/2005", then the equivalent date
    >string constructed from dtToday will be given by the script below.
    >Note that date & month less than "10" need a leading zero and that
    >getFullYear() is not supported on all browsers, so you may need to use
    >getYear and deal with it's eccentricities:


    Aha - a greengrocer !

    getYear()%100 is reliable, so those who expect to lose interest before
    AD2100 can often work with 2 year digits. I expect you have seen how
    getFullYear can be emulated using getTime and any getYear such that
    getYear()%100 is reliable.


    >function checkZero(a){
    >return (a < 10)? "0" + a : a;
    >}


    Not a well-named function. Note that it assumes a non-negative integer
    input, which is reasonable in this case. Also that it sometimes returns
    a string and sometimes a number (assuming input is number); that is
    usually harmless and harmless in this case - but if the intended format
    were YYYYMMDD [*] then the results would be quite different for
    day/month<10, as addition can occur.

    [*] For YYYYMMDD, (getFullYear()*100+getMonth()+1)*100+getDate() is
    better.


    >function todayDate() {
    >var dtToday = new Date();
    >var dtTodayString = checkZero(+dtToday.getMonth()+1)


    First + is superfluous.

    >+ '/' + checkZero(dtToday.getDate())
    >+ '/' + dtToday.getFullYear();
    >alert(dtTodayString);
    >}



    >I appologise for baddly formatted code, but Google Groups insists on
    >removing all leading spaces, so there's not much I can do.


    Don't use it, then. Example code NEEDS to be properly formatted. Find
    a competently-designed news system. Even Developersdex is, IIRC,
    better.

    --
    © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
    <URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
    Dr John Stockton, Jan 25, 2005
    #4
  5. Guest

    No, not a form element...rather a DIV

    <div id="" name="" contenteditable></div>

    I need to be able to set focus on in order to paste into it...
    IE is fine ... FF ... not so much.
    , Jan 26, 2005
    #5
  6. Randy Webb Guest

    wrote:
    > No, not a form element...rather a DIV
    >
    > <div id="" name="" contenteditable></div>
    >
    > I need to be able to set focus on in order to paste into it...
    > IE is fine ... FF ... not so much.
    >


    Thats because FF does not support "contenteditable" and as such does not
    support the concept of "focus" on it.

    --
    Randy
    comp.lang.javascript FAQ - http://jibbering.com/faq
    Randy Webb, Jan 26, 2005
    #6
  7. Guest

    Any suggestions on how to get something like that to work in FF too?
    , Jan 27, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave L
    Replies:
    3
    Views:
    3,152
    Göran Andersson
    Mar 4, 2010
  2. wk
    Replies:
    5
    Views:
    157
    Dietmar Meier
    Apr 22, 2005
  3. Martin
    Replies:
    3
    Views:
    133
  4. Replies:
    4
    Views:
    151
    Jonas Raoni
    Nov 21, 2006
  5. Roger
    Replies:
    3
    Views:
    299
Loading...

Share This Page