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>
2
  • You may want to read up on javascript -- your page will display <p>True</p><p>True</p> no matter what the condition is (and assuming each of the script blocks were valid, which they're not) Commented May 23, 2010 at 21:07
  • Even if this were possible, it appears that this would be embedded JavaScript, which is messy and not good practice. Commented May 23, 2010 at 21:27

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

17 Comments

That is invalid in HTML and XHTML. In HTML you need to escape the / characters. In XHTML you need to w3.org/TR/xhtml-media-types/#C_4
@David - you're going to be that picky and not even care about the use of single-quotes for the language attribute? ;)
@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 :)
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.
%3Ctag%3E is what should be used. And type='text/javascript' instead of language of course :)
|
1

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

Comments

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

1 Comment

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).
-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; ?>

=)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.