PDA

View Full Version : Javascript Termination



DarkBlood
August 8th, 2007, 09:42
I was wondering... why can't JavaScript act like PHP and temporarily terminate?

PHP Does it:
<?php
if($thisEM == true) {
// Output the HTML, we don't need to use an echo statement.
?>
<table>
<tr>
<td>
<div>blah, blah, blah.</div>
</td>
</tr>
</table>
<?php
}
?>

JavaScript Can't Do it:
<script language="JavaScript">
if(thisEM = true) {
// Output the HTML...
</script>
<!-- Insert HTML -->
<script language="JavaScript">
}
</script>

JavaScript will complain it was expecting a } right before the first Script code terminates.

Yun
August 8th, 2007, 23:54
Eh? Because it's made like this? And they're made seperately?

document.write is an alternative :).

And by the way


if(thisEM = true) { should be
if(thisEM == true) { I believe.

DarkBlood
August 9th, 2007, 13:41
That's a dirty way of doing it Yun, I'd rather have it how PHP does it. JavaScript dislikes the == operator by the way.

Troy1
August 9th, 2007, 13:42
If you put something event driven (on click for example) in the head then it's effectively terminated until that event occurs.

DarkBlood
August 9th, 2007, 13:45
If you put something event driven (on click for example) in the head then it's effectively terminated until that event occurs.

I know that, but the method I'm speaking of won't work for JavaScript; but it will work for PHP. I want to know why it won't for JavaScript.

Troy1
August 9th, 2007, 13:48
Because PHP actually runs and outputs all this stuff at the same time, like CGI. To do what you want, you'll have to make the output a form that you can change the value of, or you have to refresh the page.

DarkBlood
August 9th, 2007, 13:52
Because PHP actually runs and outputs all this stuff at the same time, like CGI. To do what you want, you'll have to make the output a form that you can change the value of, or you have to refresh the page.

I thought AJAX could do that. However, since AJAX is JavaScript, it still won't allow what I want JavaScript to do.

Troy1
August 9th, 2007, 13:57
Yeah, AJAX can do that, the main deal with AJAX is that it can even exchange data with the server without refreshing. You use httrequest or iframe for this.

DarkBlood
August 9th, 2007, 14:12
Yeah, AJAX can do that, the main deal with AJAX is that it can even exchange data with the server without refreshing. You use httrequest or iframe for this.

But we don't like iFrames. :p

Troy1
August 9th, 2007, 14:15
No we don't. Ajax uses ones that are invisible (IE have dimensions of 0,0,0) that they refresh separately from the main page when they need to exchange the data. Httrequest basically opens a connection through which you can send an HTTP request packet to the server while staying on the same page. It's more elegant, but you have to know HTTP headers which some people (god only knows why) find difficult to understand.

DarkBlood
August 9th, 2007, 14:22
I still haven't gotten an answer to my original question though.

Troy1
August 9th, 2007, 14:24
It's a different language. I don't know what to tell you, it's how your web browser was written!

EDIT- I would argue that a better question would be "Why can't PHP terminate like JavaScript". PHP is all processed at the same time, the tags are just a way to saperate it from HTML as everything outside basically is treated as if it were echoed.

DarkBlood
August 9th, 2007, 17:12
It's a different language. I don't know what to tell you, it's how your web browser was written!

EDIT- I would argue that a better question would be "Why can't PHP terminate like JavaScript". PHP is all processed at the same time, the tags are just a way to saperate it from HTML as everything outside basically is treated as if it were echoed.

No, that's not what I want to ask. Maybe I should bold the stuff so you can see what I mean.

<?php
if($thisEM == true) {
// Output the HTML, we don't need to use an echo statement.
?>

Notice that above, I haven't terminated the IF Statement. JavaScript CANNOT do that. It will complain that it was expecting an ending curly bracket. I want to know why JavaScript can't allow me to leave an IF Statement un-terminated before terminating the script tags.

Decker
August 9th, 2007, 17:20
Try the sun.com developers network is your best bet.

They have the answers everyone else can only guess at :D

Troy1
August 9th, 2007, 17:28
They have the answers everyone else can only guess at

Ask them why they haven't sold out yet to IBM.