views:

95

answers:

4

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>
+1  A: 

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

Matt
+4  A: 

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
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
David Dorward
@David - you're going to be that picky and not even care about the use of single-quotes for the `language` attribute? ;)
Matt
@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
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.
David Dorward
%3Ctag%3E is what should be used. And `type='text/javascript'` instead of `language` of course :)
Sean Kinsey
@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
Yes, but using deprecated stuff in a question isn't as big a deal as using invalid stuff in an answer.
David Dorward
@David - You want XHTML valid, and HTML 3.2 attributes at the same time...make up your mind?
Nick Craver
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)
David Dorward
@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
Maybe you should look back at my initial comment. Having the sequence `</p>` 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.
David Dorward
@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
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.
David Dorward
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 `<style type="text/css">`, you can just use `<style>`
ItzWarty
@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 `<style>`, [the *default* is `"text/css"`](http://www.w3.org/TR/html5/semantics.html#the-style-element), but any MIME type is fair game.
Nick Craver
@Nick: But text/javascript and text/css are the only applicable mime types. So you're basically only given 1 choice.
ItzWarty
@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
A: 

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

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.

zdawg
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).
David Dorward