Link to home
Start Free TrialLog in
Avatar of MDauphinais1
MDauphinais1

asked on

Break up a javascript IF THEN statement with HTML

OK, in PHP you can do something like this:

<?
if (something == 1) {
?>
<table><tr><td><font size="1">Regular HTML goes here </font></td></tr></table>
<?
} else {
?>
<font size="2">More HTML goes here</font>
<? } ?>

Basically, you can break up the PHP code with regular HTML code in between. Can I do the same thing with a javascript IF THEN statement?

I am asking because I use a website system that lets me include HTML templates like this:  %%TEMPLATE:mytemplatefile%%   which automatically inserts the html stored in the "mytemplatefile.html" file. (Not exactly sure what language this is called but it uses CGI).

I am trying to write a javascript code that says this:

<script type="text/javascript">
if (data=1) {
document.write('%%TEMPLATE:mytemplatefile%%');
}
</script>

But the HTML located in the mytemplatefile.html file is causing javascript problems when it comes through. So, if my original question is not possible, where you could break up the javascript like you can with PHP, then how can I phrase the javascript so that I can include this HTML without the HTML causing javascript errors?
SOLUTION
Avatar of Affiliated_IT
Affiliated_IT
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If you want to do that, I guess the way you would is by creating the html in your javascript
Avatar of MDauphinais1
MDauphinais1

ASKER

I am trying to use document.write but I get an error "Expected ; " 

The code inside of this .html file that is being included is strictly HTML.... there is no javascript or any other type of language.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Me and Affiliated answered at the exact same time on this.  Please award points accordingly.  Every other submission is just writing some code which we already stated was the solution.
silemone, I do not think it is your place to advise the user how to award the points or pass judgment on what the subsequent posts are; the first post has syntax errors due to quotes that have not been escaped and you did not provide any concrete code solution. The subsequent post after yours all add useful information, mine how to escape the quotes, pmessana how to avoid quotes, and snrudda's how to substitute quotes with single quotes, all valid information that one should be aware of.
Sorry this took so long. The site was having other issues so I had to wait for them to be resolved before I could go back to messing with this.

Let me explain why I awarded points the way I did:

In my question I was obviously already using the document.write function with the html inside. So telling me to just use document.write did not answer the question. I apologize if I was not clear with what the "'%%TEMPLATE:mytemplatefile%%" was producing... it is taking all of the html in whatever file it refrenences and inserts that into the document.write function so it is the same as entering document.write("<html><head>")...etc.

I specified that the code in the template was causing javascript to error.  LordOfPorts, pmessana, and snrudda were all right with their solutions to remove all quotes from the template file or just use single quotes and eliminate any single quotes from within the template. The ability to backslash all quotes would work but not in this case because the template is used in other situations when not being called by javascript so that would mess the HTML up.

Affiliated_IT clearly specified that I cannot emulate the PHP style coding with javascript.

I appreciate the help with this.