Problem:
I need to add a .css and .js file based on the user logged in. I have the css class attributeds added to all of the elements I need to style. What I need to do now is add the .css and .js file to the page on page load based on logged in user.
Solution:
But keeping JavaScript and CSS functions in a separate file (a
For instance, a
and for including a .css file same code should rewrite as follows:
Once the
I need to add a .css and .js file based on the user logged in. I have the css class attributeds added to all of the elements I need to style. What I need to do now is add the .css and .js file to the page on page load based on logged in user.
Solution:
The conventional way to loading external JavaScript (ie: .js) and CSS (ie: .css) files on a page is to stick a reference to them in the <head> section of your page, for example:
<head> <script type="text/javascript" src="myscript.js"></script> <link rel="stylesheet" type="text/css" href="main.css" /> </head>
But keeping JavaScript and CSS functions in a separate file (a
.js
and .css file) is highly recommended. Once they are in a separate file and part of a project, the file can be imported into a page using RegisterClientScriptBlock method.For instance, a
.js
file can be included in an ASP.NET page using the following code:Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "MyScript", "<script language=javascript src='MyJSFile.js'>");
and for including a .css file same code should rewrite as follows:
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "MyCSS",
"<link rel=\"stylesheet\" type=\"text/css\" href=\"css/MyCSSFile.css\"/>");
Once the
.js
file or .css is imported into the ASP.NET page, any of the JavaScript functions, css class and selectors can be called as before.
No comments:
Post a Comment