Showing posts with label javascript. Show all posts
Showing posts with label javascript. Show all posts

Monday, June 2, 2008

Internet Explorer (IE) Conditional Comments

It's rather common that the same CSS styles are rendered differently among different browsers. Many times there is a difference between the way Netscape/Mozilla/Firefox browsers interpret CSS code and the way IE does. Opera seems to fall on either side depending on the property. Another problem that often arises is that different Javascript code must be written for different browsers.

Now with Javascript, unlike CSS, it's easy to check the type of browser and execute different code accordingly as I've explained before. However, it's sometimes more desirable to include different Javascript files depending on the browser especially when there is code that is specific to only IE. One viable solution for this is using the IE conditional comments.

Using IE conditional comments, one can include Javascript, CSS, and HTML that is only parsed if the user browser is IE or if it's not IE. If it is IE, you check even further and include the code if browser is a specific version of IE.

There are two types of conditional comments. One is called "downlevel-hidden," which is only parsed by IE because it uses special IE syntax. The second is called "downlevel-revealed," which is ignored by IE and parsed by everything else because it's not a standard HTML comment.

The downlevel-hidden syntax is as follows:
<!--[if IE]> Javascript/CSS/HTML <![endif]-->

As you can see the downlevel-hidden syntax uses standard "<!-- ... -->" HTML comment syntax.

Now the downlevel-revealed syntax is:
<![if !IE]> Javascript/CSS/HTML <![endif]>

Here we don't have the two dashes "--" after the "!" and before the ">".

The difference between downlevel-hidden and downlevel-revealed is that downlevel-hidden is parsed by IE when the condition is true and is never parsed by other browsers. Downlevel-revealed is always parsed by other browsers and is parsed by IE only when the condition is satisfied. So in both cases IE selectively executes but in downlevel-hidden other browsers never see the code and in downlevel-revealed they always parse it.

Here are some examples of what can be done with conditional comments from MSDN
<!--[if IE]><p>You are using Internet Explorer.</p><![endif]-->
<![if !IE]><p>You are not using Internet Explorer.</p><![endif]>

<!--[if IE 7]><p>Welcome to Internet Explorer 7!</p><![endif]-->
<!--[if !(IE 7)]><p>You are not using version 7.</p><![endif]-->

<!--[if gte IE 7]><p>You are using IE 7 or greater.</p><![endif]-->
<!--[if (IE 5)]><p>You are using IE 5 (any version).</p><![endif]-->
<!--[if (gte IE 5.5)&(lt IE 7)]><p>You are using IE 5.5 or IE 6.</p><![endif]-->
<!--[if lt IE 5.5]><p>Please upgrade your version of Internet Explorer.</p><![endif]-->

<!--[if true]>You are using an <em>uplevel</em> browser.<![endif]-->
<![if false]>You are using a <em>downlevel</em> browser.<![endif]>

<!--[if true]><![if IE 7]><p>This nested comment is displayed in IE 7.</p><![endif]><![endif]-->

Some of the CSS differences I've noticed is that the opacity property has no effect in IE but it works in Firefox and Opera. When word-wrap property with value of "break-word" is applied to hyperlinks (anchors) in Firefox, it does nothing but it works on IE and Opera.

Sunday, March 30, 2008

Checking OS and Browser with Javascript


I've ran into cases where different browsers and even the same browsers but on different OS's respond differently to CSS attributes. So in some cases it's best to check for the type of OS and browser and apply attributes accordingly. I recently had to deal with this issue for the Firefox browser on Mac.




It turns out that having an HTML element with a CSS opacity attribute interferes with Flash graphics on the web page if it lays over it. As far as I noticed this only happens currently when using Firefox on Mac. Since opacity wasn't a requirement and wasn't of importance I decided to take it out on that browser/OS combination. This is done by code like the following:





function checkOSBrowser() {
if(navigator.platform.indexOf("Mac") || navigator.userAgent.indexOf("Firefox"))
document.getElementById("myElem").style.opacity = "0.8";
}




To review, navigator.platform returns the current OS and navigator.userAgent returns the running browser name. It is generally better to use indexOf() or search() to look for the desired OS or browser than to use an exact match because if anything slightly changed with an exact match the match would fail. Take "Mac" for example. If you run navigator.platform on a Mac machine with Intel processors, the output would be "MacIntel" not "Mac." So if you don't care exactly what kind of Mac is running you should just use the indexOf() or search() functions provided by the Javascript String.

Friday, February 15, 2008

Javascript Naming for CSS Float Property

Typically, you can access a CSS property of an element in Javascript using something similar to:


elem.style.propertyName = "value";


Where the propertyName is the property name in CSS with no dashes and written in camel format. For example text-align becomes textAlign. However, for the CSS float property this is a problem because float a keyword in Javascript. Therefore instead you have to use cssFloat.

Monday, February 4, 2008

Submitting a Form upon Pressing the Enter Key


It is very common on web forms that a page is desired to submit its form when a user presses the Enter key while the focus in a certain or any text box. This is typically done using JavaScript. I want to share how it's done as it can be very useful. In essence all that is done is that every time a user presses a key, a JavaScript code checks if the key pressed is the Enter key. If so it submits the form to the server, otherwise it does nothing. The following is a snippet of the code would look like:




On the JavaScript side


function enter_submit(pevent)
{
if(window.event) //Internet Explorer
{
if(myEvent.keyCode == 13) //check enter was pressed
{
document.formname.submit();
//or __doPostBack("",'');
return;
}
}
else if(pevent.which) // Netscape/Firefox/Opera
{
var pressed = pevent.which;
if(pressed == 13)
document.formname.submit();
//or __doPostBack('','');
}
}



On the HTML or ASP .NET side



<asp:Textbox ID="txtPassword" runat="server" onkeypress="javascript:enter_submit(event);/>

<input id="inPassword" type="password" onkeypress="enter_submit(event);"/>

Tuesday, January 22, 2008

asp:Button and Javascript

It is useful many times to decide the behavior of a button based on some client side code result. For example, when a button is pressed a web page might need to execute Javascript code to validate some user input or to have the user confirm a certain action and based on the output of the client code either post back to the web server or ignore the click or execute more client side code.

Let's take the scenario of asking the user to confirm a certain action as this scenario has actually come up with me at a client. The web page has a button that deletes an object permanently. Upon a user click on the button, the page executes Javascript code to open a confirmation box with an OK and a Cancel button. The confirmation box should at the least minimize accidental deletions. If the user clicks OK the page posts back and the object is deleted. However, if the user clicks Cancel the button click is ignored and nothing happens. Let's take a look at the code behind this:

Javascript part:

<script type="text/javascript">
function confirmDelete() {
return window.confirm("You really want to delete all your work?");
}
</script>


ASPX side:

<asp:button id="btnDelete" onclick="btnDelete_Click" runat="server" text="Delete" tooltip="Permanetly remove this item" onclientclick="javascript:return confirmDelete();" />


That's it.

Sunday, January 6, 2008

Submitting Files to Multiple Sites

An issue came up recently at my client where the client wanted to keep a backup copy of every file submitted by a user to a third-party site. The backup copy was going to be saved a separate server. The solution I implemented was some simple JavaScript code. Essentially, the code submits the form to one site then changes the "action" and "target" properties of the form and submits it again to the other site. The new target could be an HTML iframe or a new window. In my case it was a small popup window. Here is a sample of the code:


function submitForms() {
window.open("", "smallPopup", "location=1, status=1, scrollbars=1, width=100, height=100");
var theForm = document.myForm;
theForm.action = "http://firstSite.com/backup";
theForm.target = "smallPopup";
theForm.submit();

theForm.action = "http://2ndSite.com/receive";
theForm.target = "_self";
theForm.submit();
}

Now you might wonder why I didn't just create another form and have two forms, one for each server. The problem with that is the user would have to select the file twice, once for each form. This is because for security reasons, it is not possible to programmatically set the file path in an HTML input of type file. So a programmer is prevented from assigning the path of one input tag to be the same as another input tag of type file. A programmer only has read access to the file path. Of course this makes a lot of sense too because if it were possible then any malicious site would be able to upload any file its owner wishes from your computer to his server.

Saturday, December 8, 2007

Modifying CSS Styles with JavaScript

I recently came a cross problem at a client where I needed the page content to change upon a button click before the page is posted back. For example, imagine you have a page used to upload files. You want the page to display something like "Your file is being uploaded" right when the user clicks the upload button. Then the page is posted back while the desired message is displayed. The solution I came up with utilized JavaScript to modify the Cascading Style Sheets, CSS, of the page and then post.
The solution was as simple as adding multiple wrapping div's around the content. This could actually be done with only two div's but for my page I need three. The div that had to be shown when the page is first requested had a default CSS Display property value of "block." The div that was to be shown after the user clicks a button had Display value of "hidden." In the code that executed following the button click as shown below, one div is being hidden, one is modified to be shown, and lastly the page is posted. The JavaScript code I had was similar to the following:


function btnClick()
{
document.getElementById("initialDiv").style["display"] = "hidden";
document.getElementById("secondDiv").style["display"] = "block";
document.form1.submit();
}