URL Parameters for Dynamic JS and CSS
10
This snippet allows an aspx page (c#) to accept URL parameters to allow styles or script to be dynamically appended to the rendered page.
I wrote this simple snippet to allow one of our vendors to accept a style override so that their application would morph into something more closely resembling our own portal application. Please see screenshots for clarification.
Original Vendor Application
Vendor Application with Style Override via URL
I wrote this simple snippet to allow one of our vendors to accept a style override so that their application would morph into something more closely resembling our own portal application. Please see screenshots for clarification.
Original Vendor Application
Vendor Application with Style Override via URL
<%
if (Request.Url.AbsoluteUri.ToLower().IndexOf("style")>-1)
{
string style= Request.QueryString["style"];
Response.Write("<link rel='stylesheet' type='text/css' href='"+ style + "'/>");
}
if (Request.Url.AbsoluteUri.ToLower().IndexOf("script")>-1)
{
string script= Request.QueryString["script"];
Response.Write("<script type='text/javascript' src='"+ script+ "'></script>");
}
%>






The rationale for even adding this was that our W-2 vendor did not want to re-write some oftheir source to allow our styling to persist via url, this meant that their links should all have the style appended to them. That seemed like a bit of work, so I suggested using the dynascript - and I would do all the heavy lifting of code via javascript.
After some conversations, we decided not to go that route...Although a good idea for us as we have an established trust and reuqests can only come from our portal for direct access to their application, adding this logic would open them up for anyone to supply js at the url, so that was a bad idea...
We finally got them to do url rewrites on their links, and even though their rendered code is table based and poorly formatted, I was able to override the styles and position elements in a nice looking and usable (familiar) fashion.
Enjoy!
I have updated this, see the modified code below.
<%
foreach (object Key in Request.QueryString)
{
if (Key.ToString().ToLower().Trim() ="style")
{
string style= Request.QueryString.Get(Key.ToString().Trim());
Response.Write("<link rel='stylesheet' type='text/css' href='"+ style + "'/>");
}
}
%>