Dynamically Load a Style Sheet

Dynamically Load a Style Sheet

Question:

How do you dynamically load a style sheet? I’m trying to use the tag in the to load a style sheet. All the pages in my site use the same so I get a uniform appearance throughout. I want to let users choose which style sheet to use, with a simple form box, and have the SRC attribute of the tag in each page changed to whatever style sheet they choose. I’ve tried various things with JavaScript and cookies, but I can’t figure it out. Is it even possible? I’m designing for Internet Explorer 4 and Netscape 4.

Answer:

Trying to develop for both Netscape and IE can be frustrating at best. One solution is to make use of a query string to pass the style sheet location, then build the link statement dynamically:

	

The getQueryValue function returns the result of a token in a query string. In the example, the query string includes the token cssfile, which contains the path to the style sheet. You can set this value using a select/option list with the names being titles for each of the style sheets, and the values being the actual file references:

When sent as a GET, the name of the select tag ("cssfile") becomes one token in a queryString:

nextPage?cssfile=deepBlue.css

A similar method could be done using cookies?retrieve the cookie, parse out its value, then use document.write() to create the LINK tag in the HTML code. If you have access to server-side code like ASP or Perl, you can send this information in a form once then store it on the server until the value changes or the session ends.

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes