Customize div styles according to the website domain

I want to dynamically change the header of my website based on whether it is in dev QA or production environment. Below is the HTML code:

<div id="wrapper">
  <form id="form1" runat="server">
   <div class="wrapper">
    <div>
      <div id="siteHeader" runat="server">FTP Forms</div>
      ...other stuff
    <div>
    <div class="wrapper">
        <asp:ContentPlaceHolder id="MainContent" runat="server">

        </asp:ContentPlaceHolder>                     
    </div>
   </div>
  </form>
</div>

In my .css file, I have included the relevant CSS styles for #siteHeader based on different environments.

#siteHeader.header {
    background-color: forestgreen;
    color: white;
    font-size: 24pt;
    font-weight: bold;
    padding: 5px;
    border-bottom: 1px solid #000;
}

#siteHeader.headerQa {
    background-color: yellow;
    color: white;
    font-size: 24pt !important; // Tried using !important without success...
    font-weight: bold;
    padding: 5px;
    border-bottom: 1px solid #000;    
}

siteHeader.headerPrd {
    background-color: red;
    color: white;
    font-size: 24pt;
    font-weight: bold;
    padding: 5px;
    border-bottom: 1px solid #000;
}

I have attempted various methods to achieve this functionality but faced challenges with applying CSS changes. Here are some things I have tried:

    string hostName = Request.ServerVariables["Server_Name"];

    if (hostName.ToUpper().Contains("DEV") || hostName.ToUpper().Contains("LOCALHOST)) 
    {
        siteHeader.InnerText = "FTP Forms -Development";
        siteHeader.Attributes["class"] = "header";
        Page.Title = "FTP Dev";
    }
    else if (hostName.ToUpper().Contains("STAGE") || hostName.ToUpper().Contains("LOCALHOST"))
    {
        siteHeader.InnerText = "FTP Forms - QA";
        siteHeader.Attributes["class"] = "headerQa";
        Page.Title = "FTP QA";
    }
    else
    {
        siteHeader.InnerText = "FTP Forms - Production";
        siteHeader.Attributes["class"] = "headerPrd";
        Page.Title = "FTP Prod";
    }

I also attempted to use JavaScript to change the styling based on the hostname, but encountered issues with applying the CSS:

<script type="text/javascript">
    var hostName = location.hostname;
    if (hostName.indexOf("dev") > -1) {
        document.getElementById("siteHeader").className = "header";
        document.getElementById("siteHeader").innerText = "FTP - Forms Development";
    } else if (hostName.indexOf("stage") > -1) {
        document.getElementById("siteHeader").className = "headerQA";
        document.getElementById("siteHeader").innerText = "FTP - Forms QA"
    } else  {
        document.getElementById("siteHeader").style = "background-color: red; color: white; font-size: 24pt; font-weight: bold; padding: 5px; border-bottom: 1px solid #000;";
        document.getElementById("siteHeader").innerText = "FTP - Forms Production"
    }
</script>

Answer №1

To solve the issue in my application, I customized the Page.Render() method within my Site.Master.cs file.

protected override void Render(HtmlTextWriter writer)
{
    string hostName = Request.ServerVariables["Server_Name"];

    if (hostName.ToUpper().Contains("DEV")) // Checked locally for each condition.
    {
        siteHeader.InnerText = "FTP Forms - Development";
        siteHeader.Attributes["class"] = "header";
        Page.Title = "FTP Dev";
    }
    else if (hostName.ToUpper().Contains("STAGE") || hostName.ToUpper().Contains("LOCALHOST"))
    {
        siteHeader.InnerText = "FTP Forms - QA";
        siteHeader.Attributes["class"] = "headerQa";
        Page.Title = "FTP QA";
    }
    else
    {
        siteHeader.InnerText = "FTP Forms - Production";
        siteHeader.Attributes["class"] = "headerPrd";
        Page.Title = "FTP Prod";
    }
    base.Render(writer);
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Iterating Through Array with Node JS Before Adding a New Property

There is a JSON input with data connected to a secondary model called Users. To process this, I need to iterate through listingData.Agents to extract the index ID and then use this ID to find the corresponding user. The challenge here is that due to asynch ...

"Utilize jQuery to superimpose an image on top of

I'm attempting to create a feature where clicking on an image will reveal text underneath, similar to how retailmenot.com displays coupon codes. In addition, when the image is clicked, users should be directed to an external URL. Here is an example o ...

webstorm error: unresolved function or method when using npm azure-storage modules

Encountering a problem with WebStorm IDE when using azure-storage library as it is unable to find the correct methods for intelligent coding (although the code runs without errors). View image of unresolved function or method Appreciate any help you can ...

What is the best way to align my grid cells to the center?

I'm facing an issue where the grid cells do not move closer together as I resize the window, instead they just stay in their place. I want to achieve a similar layout to . To share my project code, I have uploaded it on codepen: https://codepen.io/Ale ...

axios does not distinguish between response and error in its return value

I have created a React component that contains a function called onFormSubmit. This function calls another function from a separate component, which in turn makes a POST request using axios. I want the ability to return a response if the POST request is su ...

Using React Native with TypeScript to Select the Parent and Child Checkboxes within a FlatList

My objective is to ensure that when a user selects a checkbox for one of the parent items ('Non Veg Biryanis', 'Pizzas', 'Drinks', 'Desserts') in the flatlist, all corresponding child items should also be selected au ...

Trouble with Google Bar Chart's Background Color not Updating

I'm having issues with the background color in my google Material Charts static api library. Despite entering a specific color code, the change is not being reflected when the page loads. These are the options I have set: var options = { b ...

Refreshing data attribute following an ajax request

I have this page with a list of various job listings. Each job listing has a button labeled "Paid" that includes a data-paid attribute indicating whether the job is paid or not. Whenever the "Paid" button is clicked, it sends a request to my route which u ...

Using Python with Selenium and JavaScript for Logging in

When using Selenium and Python, I am attempting to log in to a website but encountering difficulties. The issue is that the source code does not display the necessary Id=apple_Id required for sending login information, although it can be seen when inspecti ...

Designing a layout with one box on top and two beneath it, covering the entire page, can be achieved by following these steps

https://i.sstatic.net/A1eUh.png I am currently working on a project where I want to divide the screen into three sections. One section will cover half of the screen to display an image slider, and the remaining half will have two sections which is already ...

In the MUI v5 framework, when using nested modals, both the parent and child modal instances will close simultaneously

Here is the reproduction of my issue on codesandbox: https://codesandbox.io/s/trusting-babbage-ovj2we?file=/src/App.js A nested modal has been created where the parent modal opens a button leading to the child modal. The useState of the parent modal has b ...

Consistently encountering issues when attempting to submit JSON data via POST request (body in raw format

I'm facing an issue with sending data to my server. Currently, I am working on a project using react native and axios version ^0.16.2. let input = { 'longitude': -6.3922782, 'latitude': 106.8268856, 'content': &apos ...

Tips for retrieving javascript-generated HTML content

Currently, I'm attempting to retrieve article headlines from the NY Times website. Upon inspection, it appears that the HTML is being generated by Javascript since it's only visible when using the 'inspect element' feature in Firefox. ...

Resizing a webpage to fit within an IFRAME

Having just started learning HTML, CSS, and JavaScript, I am attempting to incorporate a page as a submenu item on my website. Below is the code that I have so far: <!DOCTYPE html> <html> <meta name="viewport" content="width=1024"> ...

Function is raising an error with the wrong value or text

I am currently testing a form that I am in the process of developing. My goal is to have different values returned each time I click on a different item from the dropdown menu. If this explanation isn't clear, please take a quick look at my pen for cl ...

Is it possible to input a value into the "data-" field from the code behind of an ASP.Net WebForm?

My goal is to update the "data-pagecount" attribute of a span tag with an ID of "searchResultPager" using code behind. Here is what my current HTML looks like: <span id="searchResultPager" runat="server" data-pagecount="2"> I am unsure on how to m ...

Having issues with media queries functioning incorrectly in VS Code

`@media(min-width:300px)and(max-width:400px){ div{ background-color: pink; } }` While experimenting with responsive design techniques, I attempted to implement this code snippet. However, it did not work as expected in Visual Studio Code. ...

Setting up AngularJS can be a pain

Greetings, my name is Rahim. While setting up AngularCLI, I ran into the following issue: 'ng' is not recognized as an internal or external command, operable program or batch file. C:\Users\ASUS ROG>ng --version 'ng' is ...

What is the best way to group all matched objects from an array based on multiple keys?

const data = [ { amount:10, gameId:7 , consoleId:3, id: 1 }, { amount:5, gameId:18 ,consoleId:3, id: 2 }, { amount:5, gameId:18 ,consoleId:3, id: 3 }, { amount:10, gameId:7 ,consoleId:3, id: 4 ...

What strategies can I employ to optimize this code in RXJS and Angular?

Is it possible to streamline these nested arrays for more efficient execution after all subscriptions have been completed? I believe there may be a solution involving the use of pipes, mergeMaps, concatMaps, etc. this.teams = [ { Assignments: [{Id: ...