Results 1 to 3 of 3

Thread: This is driving me mad

  1. #1
    Join Date
    Jul 2005
    Posts
    101
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default This is driving me mad

    Please, please help befor I go completely mental.

    What is wrong with this script. When I run it as pasted here it shows the table in the browser. But if I uncomment a second line in the first part of the 'If' clause I get a blank screen. It does not matter which line in the first part is umcommented as long as there is only one 'active' line. Since the 'else' bit always displays the table, I cut and pasted the write clauses from here into the
    'if' part and still get the problem.

    <table>
    <tr>
    <script language="JavaScript">

    var path = window.location.href;
    if (path.indexOf("build")!=-1)
    document.write('<th class="h1" align="left">buso</th>');
    //document.write('<th class="h2">Tel (+34)</th>');
    //document.write('<th class="h3">Languages</th>');
    //document.write('<th class="h4" align="left">S R C F</th>');
    //document.write('<th class="h5">Email</th>');
    //document.write('<th class="h6">Web</th>');
    else
    document.write('<th class="h1" align="left">buso</th>');
    document.write('<th class="h2">Tel (+34)</th>');
    document.write('<th class="h3">Languages</th>');
    document.write('<th class="h4" align="left">S R C F</th>');
    document.write('<th class="h5">Email</th>');
    document.write('<th class="h6">Web</th>');

    </script>
    </tr>
    </table>

    I just cannot figure it out. Thanks in advance for keeping me sane.

    Billy

  2. #2
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by wkenny
    When I run it as pasted here it shows the table in the browser. But if I uncomment a second line in the first part of the 'If' clause I get a blank screen. [...]

    <table>
    <tr>
    <script language="JavaScript">
    Don't use the language attribute. Use the type attribute instead:

    HTML Code:
    <script type="text/javascript">
    I won't quote the rest of your code for brevity, but as far as the interpreter is concerned, it's equivalent to:

    Code:
    if(-1 != path.indexOf('build')) {
      document.write(...);
    }
    document.write(...);
    ...
    document.write(...);
    else {
      document.write(...);
    }
    document.write(...);
    ...
    document.write(...);
    In other words, very broken and the else clause is a syntax error as it doesn't have an accompanying if statement.

    To do what you're attempting, you must include block statements:

    Code:
    if(-1 != path.indexOf('build')) {
      document.write(...);
      ...
      document.write(...);
    } else {
      document.write(...);
      ...
      document.write(...);
    }
    For maintenance purposes, I recommend that you always use block statements as it saves effort should a single statement after a control structure become many.

    Mike
    Last edited by mwinter; 07-18-2005 at 10:07 PM.

  3. #3
    Join Date
    Jul 2005
    Posts
    101
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    Thanks a million, Mike. That does the trick.

    Billy

Bookmarks

Posting Permissions

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