Wednesday, February 01, 2006
by Nik Kalyani
Wednesday, February 01, 2006 11:25:26 AM (Pacific Standard Time, UTC-08:00)

Sometimes, it is necessary to override stylesheets defined on a page or to add additional stylesheets, perhaps for different media. The script below defines a JS object that allows you to define stylesheets for screen/print for IE/Other browsers. It then adds them to the page dynamically.

In the code below, you can ignore the portion from //BEGIN to //END. The snippet at the end is all that needs to be changed. Se the desired values for “picker.ieScreen,” “picker.iePrint,” “picker.otherScreen,” and “picker.otherPrint” and the code will take care of the rest.

<script language="javascript">

// BEGIN: styleSheetPicker object definition
function styleSheetPicker()
{
   this.ieScreen = this.iePrint = this.otherScreen = this.otherPrint = "";
}

styleSheetPicker.prototype.render = function()
{
   if (document.createStyleSheet)
   {
    if (this.ieScreen)
     document.createStyleSheet(this.ieScreen);

    if (this.iePrint)
        {
     var ieP = document.createStyleSheet(this.iePrint);
            ieP.media = "print";
        }
   }
   else
   {
        var head = document.getElementsByTagName("head")[0];
    if (this.otherScreen != "")
            head.innerHTML += "<link rel=\"stylesheet\" href=\"" + this.otherScreen + "\">";

    if (this.otherPrint != "")
            head.innerHTML += "<link rel=\"stylesheet\" media=\"print\" href=\"" + this.otherPrint + "\">";

   }
}
// END: styleSheetPicker object definition


var picker = new styleSheetPicker();

// IE stylesheets
picker.ieScreen = "ie.css";
picker.iePrint = "other.css";

// Other browser stylesheets
picker.otherScreen = "default.css";
picker.otherPrint = "other.css";

picker.render();

</script>

 

#    Comments [1] - Trackback    

CSS | Javascript

 Monday, January 23, 2006
by Nik Kalyani
Monday, January 23, 2006 10:48:53 AM (Pacific Standard Time, UTC-08:00)

There are many situations when you may want to dynamically add a stylesheet to a page. One use case is when a control is being dynamically loaded and it comes with its own stylesheet. If you don’t know that the style is needed in advance or are dealing with an environment where you have a stylesheet for a control but don’t have control of the page (as it is in DotNetNuke), there are two common methods in use:

1) Add a <LINK> tag: This works but leaves the parent page non-XHTML compliant since <LINK> is only valid in the <HEAD> section.

2) Add a <STYLE> element and add the styles to the page. This works but gives you a bloated page and does not take advantage of stylesheet caching.

The solution is to dynamically add a stylesheet to the page. Here’s how.

(Internet Explorer makes this easy to do with Javascript using document.createStyleSheet. For other browsers, you have to get a handle to the <HEAD> element and then insert the stylesheet.)

bool isIE = (HttpContext.Current.Request.Browser.Browser == "IE" ? true : false);
string stylesheetUrl = “/test/default.css”;
StringBuilder styleScript = new StringBuilder();
styleScript.Append("<script language=\"javascript\">");
if (isIE)
    styleScript.Append("document.createStyleSheet(\"" + styleSheetUrl + “\");");
else
   
styleScript.Append("var newStyleSheet=document.createElement(\"link\"); newStyleSheet.rel=\"stylesheet\";   newStyleSheet.href=\"" + styleSheetUrl + “\”; document.getElementsByTagName(\"head\")[0].appendChild(newStyleSheet);");
styleScript.Append("</script>");
Page.RegisterClientScriptBlock(“MyStylesheet”, styleScript.ToString());

For IE, the script adds the stylesheet directly. For other browsers, it creates a new <LINK> element on the page and then adds it at the end of list of <HEAD> elements.

 

 Saturday, January 21, 2006
by Nik Kalyani
Saturday, January 21, 2006 5:29:39 PM (Pacific Standard Time, UTC-08:00)
A new research study by Optaros, Inc. highlights a growing increase in the adoption rate of Open Source at corporations. Read the story here.
#    Comments [1] - Trackback    

by Nik Kalyani
Saturday, January 21, 2006 12:54:51 PM (Pacific Standard Time, UTC-08:00)

Frequently, you will have a situation where you store the string representation of an enumerated value in a file or database, and when it is retrieved you have to re-assign it to a variable that is of the enumerated type. You can do this as follows:

public enum MyEnumeratedType
{
     Foo = 0, Bar = 1
}

MyEnumeratedType enumVariable = (MyEnumeratedType) Enum.Parse(typeof(MyEnumeratedType), “Bar”, true);

With all due respect to Anders, this is NUTS!!! I wish there was a simpler way…

#    Comments [0] - Trackback    

by Nik Kalyani
Saturday, January 21, 2006 12:38:25 PM (Pacific Standard Time, UTC-08:00)

The DotNetNuke Newsletter goes out to over 150,000 email addresses each month. For any given edition, we get a few thousand “out of office” responses, a few hundred spam traps and a few “unknown addresses.” (The last category is small because we pro-actively eliminate bad addresses before sending out the newsletter.)

Now, about those “out of office” responses. Most of them are pretty standard — I’m out from X date to Y date and call Debbie or Stephen or Bob at this number. I don’t sit and read all of these, but I will occasionally click on a few.

It’s fun to see these written in different languages —

– Ich bin vom 21. Dezember 2005 bis 2. Januar 2006  nicht im Büro.
– Grazie per il vostro messaggio. Sarò fuori ufficio per ferie fino al 1 Gennaio 2006 incluso e non avrò accesso alla posta.
– Onze kantoren zijn gesloten van 24 december tot 1 januari. U kan ons terug bereiken op maandag 2 januari.
– Je serai absent(e) du  12-23-2005 au 01-03-2006. Je répondrai à votre message dès mon retour le 3 janvier 2006.
– Tack för ditt mail. Jag är åter på kontoret 2005-12-27 och läser ditt mail då.

Most of the time the reason given is “vacation,” but there’s always people sharing just a wee bit more information. Here are a few or the more interesting reasons:

– I’m going to be a new dad. (congrats!)
– I have been fired. (oops)
– I will be going for therapy. (wonder what it was for and how it turned out)
– I will be sun-bathing on a cruise ship. (hope you had a good time)
– I have a sudden and severe back problem. (hope it’s better now)
– I am handling a crisis in the Middle East. (hmmmm…)  
– I am getting married. (congrats!)
– I am absent due to commitments in the Swiss Military. (ah..good old conscription)
– Our company is going out of business. (wonder why the mail server is still up?)

#    Comments [0] - Trackback    

RSS feed
Search and Links
Bling

View Nik Kalyani's profile on LinkedIn

Contact me: nik*kalyani.com (replace "*")

TechBubble
www.flickr.com
This is a Flickr badge showing public photos from techbubble. Make your own badge here.
Statistics
Total Posts: 216
This Year: 34
This Month: 2
This Week: 0
Comments: 238
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008
Nik Kalyani
Sign In
All Content © 2008, Nik Kalyani