Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JS to build table rows based on If results - inside HTML form

Status
Not open for further replies.

seattlemom41

Programmer
Apr 8, 2012
2
US
I'm trying to get a document.write to work based on the results of a JS "If". w3c validator says script not allowed where it's placed. It isn't processing the If - the first document.write is done whether the if is true or false.

DOCTYPE is XHTML 1.0 Transitional, xml is v1.0, utf 8. This is being used with shopping cart software; tech support says I can only use "simple JS" in this document.

Here's the basic layout of my code with specifics of the If:

Code:
<table...> (display layout)
<tr><td><form...>
<table>
<tr>
<td>
 // Here's the script:
<script type="text/javascript">
// Don't ask for shipping info if the purchase is a download only
<!--
  var x=9
  var y=8
  if(x<y){
  document.write('<tr><td colspan="2" align="center"><input type="hidden" name="shipmethod" value="1" />(lots more hidden inputs)</td></tr>');
  }else{
  document.write.('<tr><td colspan="2" align="left"><div align="left"><input name="shipname" size="40"></div>(lots more visible inputs)</td></tr>')
}
//-->
</script>
<tr><td>(some more stuff)
</td>
</tr>
</table>
</form>
</td>
</tr>
</table>

What am I doing wrong?? Thanks in advance.
 
I can't reproduce the validation error with the code provided nor am I able to reproduce the if error either. So your actual code must have a different conditional that is always true.

In any case, document.write should not be used to generate HTML elements in that fashion. Its suggested you use the DOM creation methods such as insertRow and insertCell.

document.write will produce a completely new document each time its called after the page has loaded. Which is why its use is cautioned.

Using tables for layout is strongly discouraged as it makes site maintenance very difficult. Also in your sample code the mismatching of tags can potentially have hideous consequences.

Also you have a stray period in your second document.write statement which would prevent it from working, and cause an error.

I would also suggest using the error console of whichever browser you are using to test this, to catch any javascript errors that may be cropping up.



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Thanks, vacunita. I made a few typos in reproducing the code because it's pretty long and I shortened it to give you only the basic structure. I apologize for the confusion (such a the extra period in the 2nd document.write statement and closing a DIV that doesn't exist!) I thought I had reproduced the tables structures correctly - did I not?

When I run the actual code through Lint, I get no errors. Evidently you're right, my code *is* always returning true, even though viewing the page source shows it should return false.

Re: tables (I absolutely hate them, but haven't learned DOM), as I said the shopping cart software tech support said I can only use "simple Javascript". This page calls their .cgi script which, apparently, can only handle that. Does using the DOM method count as "simple JS"? Or do I need to ask them that?

 
I thought I had reproduced the tables structures correctly - did I not?

Apparently not:

Code:
[b]
[red]<table...> [/red]
  [blue]<tr>[/blue]
    [green]  <td>[/green]
      [red]<table>[/red]
        [blue]<tr>[/blue]
          [green]<td>[/green]
            
            [gray]<tr><td>...</tr></td>[/gray]
                                       
           [blue]<tr>[/blue]
          [green]<td>[/green]
          [green]</td>[/green]
        [blue]</tr>[/blue]
      [red]</table>[/red]
    [green]</td>[/green]
  [blue]</tr>[/blue]
[red]</table>[/red]
[/b]
Gray is the html produced by the javascript script.

Mismatched tds and trs.

When I run the actual code through Lint, I get no errors. Evidently you're right, my code *is* always returning true, even though viewing the page source shows it should return false

What is the actual conditional? Have you tried alerting the values you are comparing to confirm what they are?

as I said the shopping cart software tech support said I can only use "simple Javascript". This page calls their .cgi script which, apparently, can only handle that. Does using the DOM method count as "simple JS"? Or do I need to ask them that?

There's no way to know what they consider simple JS. I mean JS is JS. If the browser can handle it, there's no reason their shopping cart can't. But of course only they know what their shopping cart is doing and what it can handle.





----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top