PDA

View Full Version : This is driving me mad



wkenny
07-18-2005, 08:51 PM
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

mwinter
07-18-2005, 10:04 PM
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:


<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:


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:


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

wkenny
07-18-2005, 10:24 PM
Thanks a million, Mike. That does the trick.

Billy