Div not working.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • DBLWizard

    #1

    Div not working.

    Howdy All,

    I am trying to detect if the user has cookies enabled on a page and
    display text accordingly. My cookie detection works but I can't get
    the divs to work. Here is the javascript and html does anybody have
    any sugestions?

    <script language="JavaS cript">
    function window_onload() {
    var sCookie;

    SetCookie("Cook ieTest","True") ;
    sCookie = GetCookie("Cook ieTest");

    if (sCookie = "True")
    {
    divNoCookies.st yle.display = "none";
    divDisclaimer.s tyle.display = "block";
    }

    }

    window.onload = window_onload;
    </script>


    <div ID="divDisclaim er" Name="divDiscla imer" style="display: none">
    <!-- #include file='County\Di sclaimer.htm' -->
    </div>
    <div ID="divNoCookie s" Name="divNoCook ies" style="display: block">
    <tr>
    <td>
    <FONT color=red>
    Your browser or your firewall is not set up to allow
    cookies. This site requires session cookies. Please check your
    browser
    settings or contact your system administrator.
    </FONT>
    </td>
    </tr>
    </div>

  • Michael Winter

    #2
    Re: Div not working.

    On 28/03/2005 17:48, DBLWizard wrote:
    [color=blue]
    > <script language="JavaS cript">[/color]

    The language attribute has been deprecated for a very long time. Use
    the (required) type attribute instead:

    <script type="text/javascript">
    [color=blue]
    > function window_onload() {[/color]

    Rather than introducing a global identifier unnecessarily, assign the
    function object directly to the onload property:

    window.onload = function() {
    /* ... */
    };
    [color=blue]
    > var sCookie;
    >
    > SetCookie("Cook ieTest","True") ;
    > sCookie = GetCookie("Cook ieTest");[/color]

    I hope you check that the cookie property exists and is a string
    before writing to it. If not, you'll create a cookie property which
    will always contain whatever you assign to it. See
    <URL:http://www.jibbering.c om/faq/faq_notes/cookies.html>.
    [color=blue]
    > if (sCookie = "True")[/color]

    That expression will always be true as you're assigning the string,
    'True', to the sCookie variable. The comparison operator is two
    consecutive equals symbols (==). A simple way to avoid this specific
    example is to always specify literals and other "r-values"
    (expressions that you cannot assign to) first:

    if('true' = sCookie) {

    In this case, the script engine would report an error as you cannot
    assign to a string literal.
    [color=blue]
    > divNoCookies.st yle.display = "none";
    > divDisclaimer.s tyle.display = "block";[/color]

    Microsoft came up with (what I think is) a stupid idea where global
    variables are created based on the id and name attributes of HTML
    elements. This "feature" is not supported and all browsers and should
    never be used. Instead, use the getElementById method (with
    appropriate feature detection):

    var disclaimer = null,
    noCookies = null;

    if(document.get ElementById) {
    disclaimer = document.getEle mentById('divDi sclaimer');
    noCookies = document.getEle mentById('divNo Cookies');
    }
    if(disclaimer && disclaimer.styl e
    && noCookies && noCookies.style )
    {
    disclaimer.styl e.display = 'block';
    noCookies.style .display = 'none';
    }
    [color=blue]
    > <div ID="divDisclaim er" Name="divDiscla imer" style="display: none">[/color]

    DIV elements do not have name attributes.
    [color=blue]
    > <div ID="divNoCookie s" Name="divNoCook ies" style="display: block">
    > <tr>
    > <td>[/color]

    You don't seem to have a table, so you can hardly have table rows or
    cells. Even if there is a TABLE element somewhere, a DIV element
    cannot be its child. That is,

    <table>
    <div>

    is invalid, however

    <table>
    <tr>
    <td>
    <div>

    is fine. If you want to group table rows, use a TBODY element:

    <table>
    <tbody>
    <tr><!-- ... --></tr>
    <tr><!-- ... --></tr>
    </tbody>
    </table>
    [color=blue]
    > <FONT color=red>[/color]

    The FONT element is deprecated. A better approach would be to give a P
    element a warning class and style that red:

    In a style sheet:

    .warning {
    color: red;
    }

    <p class="warning" >
    ...
    </p>

    Hope that helps,
    Mike

    --
    Michael Winter
    Replace ".invalid" with ".uk" to reply by e-mail.

    Comment

    • kaeli

      #3
      Re: Div not working.

      In article <1112028488.098 073.225120@f14g 2000cwb.googleg roups.com>,
      ibflyfishin@yah oo.com enlightened us with...[color=blue]
      > Howdy All,
      >
      > I am trying to detect if the user has cookies enabled on a page and
      > display text accordingly. My cookie detection works but I can't get
      > the divs to work. Here is the javascript and html does anybody have
      > any sugestions?[/color]

      Stop coding using vbscript syntax and operators when you're writing
      javascript. :)

      (see Michael's post for a more involved breakdown of the script's many
      issues)

      --
      --
      ~kaeli~
      "When dogma enters the brain, all intellectual activity
      ceases" -- Robert Anton Wilson



      Comment

      Working...