function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Edwin VijayEdwin Vijay 

URGENT-- HTML "IF STATEMENT" IN VISUALFORCE

Hi All,
 
I have been trying to use the following HTML code in my VisualForce Page editor.
 

 

Code:
<apex:page controller="Rephome" sidebar="false" tabstyle="SPA_Detail__c" standardstylesheets="false">
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<!--[if lt IE 8]><link rel="stylesheet" type="text/css" href="{!$Resource.ie}" /> <![endif]-->
<!--[if lt IE 7]> <link rel="stylesheet" type="text/css" href="{!$Resource.ielt7}" /> <![endif]-->

My questions:

Is it possible to use DOCTYPE defenition?, i get a warning saying that DOCTYPE is not allowed.

The conditional IF statements are finally generated as comments. Is HTML Conditional IF statement not supported in VisualForce or is there some other way in which this can be achieved?

I face browser related issues while rendering the VisualForce, is it because of the Doctype? When a custom stylesheet is used for a VF page with Salesforce Header does the custom stylesheet affect the Salesforce Header as well.

Thanks,

Edwin
 

Best Answer chosen by Admin (Salesforce Developers) 
looseneuronslooseneurons

Edwin,

 

I notice two questions... one that I am also playing with is changing the DOCTYPE. There are other threads out here regarding that.

 

Meanwhile the conditional IF is failing because Visualforce pre-processes your XML and expands XML entities and comments. The simple example is if you have &amp; in your source, VF decodes it as '&'... which then fails to render in your browser. So if your source instead had &amp;amp; the first entity would be decoded to replace the '&' at the beginning of what was left over.

 

With respect to comments, you can use this to your advantage by escaping the '<' before your conditional block...

 

So if you change:

<!--

to:

&lt;!--

 

the conditional [IF IE] comment will make it through pre-processing to appear in the source.

 

Similarly you may have to escape the closing --> as --&gt;

 

HTH

--Ken 

All Answers

looseneuronslooseneurons

Edwin,

 

I notice two questions... one that I am also playing with is changing the DOCTYPE. There are other threads out here regarding that.

 

Meanwhile the conditional IF is failing because Visualforce pre-processes your XML and expands XML entities and comments. The simple example is if you have &amp; in your source, VF decodes it as '&'... which then fails to render in your browser. So if your source instead had &amp;amp; the first entity would be decoded to replace the '&' at the beginning of what was left over.

 

With respect to comments, you can use this to your advantage by escaping the '<' before your conditional block...

 

So if you change:

<!--

to:

&lt;!--

 

the conditional [IF IE] comment will make it through pre-processing to appear in the source.

 

Similarly you may have to escape the closing --> as --&gt;

 

HTH

--Ken 

This was selected as the best answer
tankgirlrstankgirlrs

I tried to do as you said and i came to two different errors depending on how i do it. If i do it the following way:

 

&lt;!--[if lte IE 6]&gt; //the code &lt;![endif]--&gt;

 it will show 'if lte IE 6' and 'endif'at the top of the page. And if i do it this way:

 

&lt;!--[if lte IE 6]> //the code <![endif]--&gt;

 it gives an error of 'The content of elements should consist of well-formed character data or markup. at line 10'.

 

Because alot of our VIP users use IE6, i need to include special instructions to the bowser so that it will load the right css file and dispaly the content of the page correctly for all browsers. 

 

Thanks!

 

 

 

 

kenfkenf

Sorry, tankgirlrs. I don't think I gave the whole answer. Using your code fragments, this is what I am using:

 

 

&lt;!--[if IE]>

// the code

&lt;![endif]--&gt;

 

I escape the entire [endif] and only escape the beginning [if IE].

 

 

 

Regards,

--k 

Message Edited by kenf on 12-09-2009 10:43 AM
tankgirlrstankgirlrs
I just tried what you suggested,

&lt;!--[if lte IE 6]>
//the code
&lt;![endif]--&gt;

but all it does is show it at the top of the page as:

 

<!--[if lte IE 6]> <![endif]--> 

 

is this working for you?

 

 

Message Edited by tankgirlrs on 12-09-2009 02:01 PM
kenfkenf

Yes. Hmmm.

 

Here is an example, straight out of the page:

 

 

&lt;!--[if IE]>

<link rel="stylesheet" type="text/css" media="print" href="{!URLFOR($Resource.css,'print_ie.css')}" />

&lt;![endif]--&gt;

 

We also use it for conditional HTC, CSS and Javascript for IE support.

 

Remember this thread started with a question  about DOCTYPE. Are you using one, or accepting the default? I have seen that makes a difference in our pages.

 

Ours is <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

With the DOCTYPE and no escaping, we get basically <!-------------------------------------->... with one dash for each letter of the code 'commented' out... weird.

 

With the escaping and no DOCTYPE, we get the other behavior you have described. 

 

Regards,

--k 

Message Edited by kenf on 12-09-2009 11:24 AM
tankgirlrstankgirlrs

Yes I believe the  DOCTYPE is the last piece I need. I searched the discussion boards and found some references to doing it in an s-control, but I can't do that since I am using VisualForce pages, or trying the following:

 

<apex:page showHeader="false" contentType="application/xhtml+xml">
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

 But that doesn't work either and many people are having issues with it. See thread: http://community.salesforce.com/sforce/board/message?board.id=Visualforce&message.id=3534

When I try it, and yours I get:

Warning:

 A DOCTYPE is not allowed in content. At line -1

 

 So are you using an s-control? Or how is it set up for you?

 

BTW thank you so much for all your help in this matter :-)

 

~Red

 

Message Edited by tankgirlrs on 12-09-2009 02:58 PM
tankgirlrstankgirlrs

BTW, the conditional HTLM tags are working now :-) just get that error since i have error messages set up for the login page, but it only shows once...wonder if i can remove it??

 

But thanks again! you have saved me from the biggest headace in trying to make IE6 work with my CSS lol

 

 ~Red

kenfkenf

Yes, I wanted to use HTML STRICT as well. This was a really painful experience this summer, so I am glad it is working for you!

 

There are other posts out here about not being able to remove message from Apexpages.getMessages() , so I have also had the same issue with the DOCTYPE warning showing all the time and have intercepted the messages myself. It is a pain, but browser compatibility is a condition of acceptance, so at least we have a way to make it work.

 

--k