0

in PHP we can put HTML between codes like this:

<?php 
if (condition) {
?>

<p>True</p>

<?php 
} else {
?>

<p>True</p>

<?php 
}
?>

can we do this in javascript ? like this ?

<script language='JavaScript'>
if (condition) {
</script>

<p>True</p>

<script language='JavaScript'>
} else {
</script>

<p>True</p>

<script language='JavaScript'>
}
</script>
  • You may want to read up on javascript -- your page will display `

    True

    True

    ` no matter what the condition is (and assuming each of the script blocks were valid, which they're not)
    – Joe May 23 '10 at 21:07
  • Even if this were possible, it appears that this would be embedded JavaScript, which is messy and not good practice. – Scott Christopherson May 23 '10 at 21:27

4 Answers4

4

There's something like this that has the effect you posted (maybe not your intention though, it's hard to say), but I wouldn't do it.

<script type="text/javascript"> //language == deprecated!
if (condition) {
  document.write('<p>True<\/p>');
} else {
  document.write('<p>True<\/p>'); //maybe False here?
}
</script>

But again this is just a demonstration of the effect, try to avoid document.write (it' a blocking operation) whenever possible.

Update: Edited based on comments below to make this example you shouldn't use! valid, but you shouldn't be copy/pasting it in the first place...

Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155
  • That is invalid in HTML and XHTML. In HTML you need to escape the `/` characters. In XHTML you need to http://www.w3.org/TR/xhtml-media-types/#C_4 – Quentin May 23 '10 at 21:05
  • @David - you're going to be that picky and not even care about the use of single-quotes for the `language` attribute? ;) – Matt May 23 '10 at 21:07
  • 2
    @David - Making this strictly XHTML compliant wasn't the point, you'll rarely see CDATA tags in answers here...that's a 100% constant implementation detail, not really relevant to the question :) – Nick Craver May 23 '10 at 21:07
  • The spec allows single quotes. In HTML 3.2 the language attribute is fine. As for my comments, there is no reason to provide broken examples. Take a comment as a suggestion for improvement, there's no need to go on the defensive and claim that teaching people with invalid code is OK. – Quentin May 23 '10 at 21:14
  • %3Ctag%3E is what should be used. And `type='text/javascript'` instead of `language` of course :) – Sean Kinsey May 23 '10 at 21:15
  • @David - HTML 4 [deprecates the `language` attribute](http://www.w3.org/TR/REC-html40/interact/scripts.html#h-18.2.2) this was about a decade ago...in any case it's irrelevant to the question :) – Nick Craver May 23 '10 at 21:16
  • Yes, but using deprecated stuff in a question isn't as big a deal as using invalid stuff in an answer. – Quentin May 23 '10 at 21:18
  • @David - You want XHTML valid, and HTML 3.2 attributes at the same time...make up your mind? – Nick Craver May 23 '10 at 21:19
  • I'd like valid *something* in *answers*. I don't really care what (so long as it answers the question). (Having people taking the time to provide valid code in their questions is a bonus and I'll go no further then a link to the validator if there is an obvious validity problem that is reasonably likely to cause the problem described) – Quentin May 23 '10 at 21:20
  • @David - I think you glossed over my answer, in which I strongly suggest **don't do this** in the first place. As for "I'd like valid something in answers"... I *did* correct his invalid HTML, notice there's no `language` attribute in my answer. It's *you* who are **assuming** he's dealing with XHTML, but this is *really*, **really** beside the point. I suggest you have a read: http://stackoverflow.com/questions/66837/when-is-a-cdata-section-necessary-within-a-script-tag – Nick Craver May 23 '10 at 21:27
  • Maybe you should look back at my initial comment. Having the sequence `` inside a script element is invalid HTML 4. (If it is outside a CDATA block then it is also invalid XHTML). I made no such assumption, I just pointed out that your example is invalid in every markup language that could be applicable to the question. – Quentin May 23 '10 at 21:29
  • @David - The backslash you have a point about updated with an additional warning, the `language` we'll disagree on, I don't think it belongs in any current webpage, even if it was valid 13 years ago, `type` however should be there. – Nick Craver May 23 '10 at 21:38
  • You seem to be putting words into my mouth. I just said that deprecated stuff in a question isn't as big a deal as invalid stuff in an answer. I don't think people should be using the language attribute either. Anyway, its a better answer now. +1. – Quentin May 23 '10 at 21:42
  • HTML5 no longer requires language="" or type="" since JavaScript is the only language used by the script tag. Similarly, you no longer need to use ` – Warty May 23 '10 at 23:15
  • @ItzWarty - That's not *entirely* accurate, the *default* `type` is `"text/javascript"`, so yes it's not **required**, [ but it's not the only thing supported](http://www.w3.org/TR/html5/semantics.html#script). The same is true for ` – Nick Craver May 24 '10 at 01:17
  • @Nick: But text/javascript and text/css are the only applicable mime types. So you're basically only given 1 choice. – Warty May 24 '10 at 03:19
  • @ItzWarty - Other types are still valid: `text/ecmascript`, `application/ecmascript`, `application/javascript`, `text/vbscript`. Rarely used, but valid...you can't say they're not there just because they're rarely used (admittedly, if ever used, after HTML5 takes hold). – Nick Craver May 24 '10 at 11:30
1

No. Browsers will output things in order that it sees them, without considering any conditions of other media on the page.

Matt
  • 43,482
  • 6
  • 101
  • 102
-1

not that i know of, but for php do this instead

<?php if (condition): ?>
<p>True</p>
<?php else: ?>
<p>False</p>
<?php endif; ?>

=)

Galen
  • 29,976
  • 9
  • 71
  • 89
-1

This works in PHP because the PHP interpreter interprets your code before sending the output to the client browser window. In other words, the actual HTML document being sent to the client is parsed and computed before being sent to the requesting browser. With Javascript mostly being a client-side scripting language this method is not possible, since you already have your HTML document generated from a server-side language such as PHP. You can manipulate the document in other ways using Javascript, with technologies such as Ajax and the DOM.

Zoli
  • 1,137
  • 7
  • 12
  • 3
    There is no reason why a client side language embedded in a document couldn't do this. It isn't possible with HTML because HTML is not designed that way, but this hasn't nothing to do with the difference between client and server. Even JavaScript is processed as the document loads (which is why document.write works). – Quentin May 23 '10 at 21:11