I have a site where 6 css files are linked, But in a page i need to use only 3 css files. My developers are using master page in .net, so they don't want to change that. So, my question is: Is there any way I can skip few css files which is linked?
It is not very 'elegant' but you can use a jQuery script to manipulate and remove the link:
<script type="text/javascript">
$(document).ready(function () {
$("link[type='text/css']").remove();
});
</script>
You can use jQuery selector to refine the search. I.e.
$("link[href='Styles/Site.css']").remove();
Another solution (doesn't need jQuery) is adding a configuration property to the master page like this:
Site.Master.cs
private bool _IncludeOtherCss = true;
public bool IncludeOtherCss {
get { return _IncludeOtherCss; }
set { _IncludeOtherCss = value; }
}
Site.Master (head section)
<%if (IncludeOtherCss)
{ %>
<link href="~/Styles/Site.css" rel="stylesheet" type="text/css" />
<%} %>
Other page:
protected void Page_Init(object sender, EventArgs e)
{
(Master as SiteMaster).IncludeOtherCss = false;
}
Related
I have read that to Log Out of the application you need to close the window and I found this code:
This answer has what you are looking for:
How to run JavaScript function from GWT Java with JSNI?
Specifically in Java:
myButton.addClickHandler(new ClickHandler(){
public void onClick(ClickEvent event) {
closeWindow();
};
});
public static native void closeWindow() /*-{ $wnd.closeWindow();}-*/;
Then in JavaScript in your app's .html page:
<script type="text/javascript" language="javascript">
function closeWindow() {
window.open('','_self','');
window.close();
}</script>
I have implemented this in my application by:
//Log Out Button
Button logOutButton = new Button("Log Out");
logOutButton.addClickHandler(new ClickHandler(){
public void onClick(ClickEvent event) {
closeWindow();
}
});
public static native void closeWindow() /*-{ $wnd.closeWindow();}-*/;
And the HTML:
<!doctype html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!-- -->
<!-- Consider inlining CSS to reduce the number of requested files -->
<!-- -->
<!-- <link type="text/css" rel="stylesheet" href="org.AwardTracker.AwardTracker.AwardTracker.css"> -->
<!-- -->
<!-- Any title is fine -->
<!-- -->
<title>Wrapper HTML for AwardTracker</title>
<!-- -->
<!-- This script loads your compiled module. -->
<!-- If you add any GWT meta tags, they must -->
<!-- be added before this line. -->
<!-- -->
<!-- script language="javascript" src="org.AwardTracker.AwardTracker/org.AwardTracker.AwardTracker.nocache.js" --><!-- /script -->
<script src="org.AwardTracker.AwardTracker/org.AwardTracker.AwardTracker.nocache.js">
<type="text/javascript">
function closeWindow() {
window.open('','_self','');
window.close();
}
</script>
</head>
<!-- -->
<!-- The body can have arbitrary html, or -->
<!-- we leave the body empty because we want -->
<!-- to create a completely dynamic ui -->
<!-- -->
<body>
<!-- OPTIONAL: include this if you want history support -->
<iframe id="__gwt_historyFrame" style="width:0;height:0;border:0"></iframe>
</body>
</html>
However, I get the following error on the lines:
closeWindow();
"The method closeWindow() is undefined for the type new ClickHandler(){}"
public static native void closeWindow() /*-{ $wnd.closeWindow();}-*/;
Multiple markers at this line
- Syntax error, insert "EnumBody" to complete BlockStatement
- Syntax error on token "void", # expected
- Syntax error, insert "enum Identifier" to complete
EnumHeaderName
Thank you to all who responded. Based on your responses...
I am using sessions like (via RemoteServiceServlet) in my app. Therefore, as per below in the responses, I need to invalidate session first followed by removal of element from dom. So tried the following:
On the client side:
logOutButton.addClickHandler(new ClickHandler(){
public void onClick(ClickEvent event) {
//Invalidate the session and then reload the application.
AsyncCallback<Void> callback = new InvalidateSessionHandler<Void>(SelectPersonView.this);
rpc.invalidateSession(callback);
}
});
class InvalidateSessionHandler<T> implements AsyncCallback<Void> {
SelectPersonView view;
public InvalidateSessionHandler(SelectPersonView view) {
this.view = view;
}
public void onFailure(Throwable ex) {
System.out.println("RPC call failed - InvalidateSessionHandler - Notify Administrator.");
Window.alert("Connection failed - please retry.");
}
public void onSuccess(Void result) {
//Reload the application.
Window.Location.assign("/");
}
}
On the server side:
public void invalidateSession() {
getThreadLocalRequest().getSession().invalidate(); // kill session
}
This seems to work. However, I am having trouble testing more than one session locally and I do not have a test server I can deploy to. So can I please ask for someone who knows what they are doing in this space to check it to ensure I am not introducing issues into production. My greatest concern is that this will log everyone out. I am particularly toey because I had a situation where sessions were not compartmentalised and users could see other people's data. This has been fixed and I do not want to break that fix!!
You cannot close a window using JavaScript if the window was opened by a user. You can only close a new window that was opened by your app.
Closing window will have no effect on user authentication as most authentication mechanisms rely on server sessions or cookies.
If your authentication is session-based, when a user clicks on the Log Out button you need to (1) invalidate user's session, and (2) reload your app, which will display default entry point for non-authenticated users (home page or login page).
Javascript can only close the page if it is opened by same script. So closeWindow() won't even work. So :
If you are not using sessions in your app i.e. you think that only closing the window is a goal to achieve. Then simply delete that iframe from DOM rather closing page. (You can do that by using js.)
document.getElementById('iframeid').innerHTML = '';
If you are using sessions like (via RemoteServiceServlet) in your app, then you need to invalidate session first followed by removal of element from dom. (For this i am not sure how to do.)
Or
Instead of removal, you can just reload the iframe (which is considered to be as a reload of your app):
document.getElementById('iframeid').src =
document.getElementById('iframeid').src
This is the final code I used:
I am using sessions (via RemoteServiceServlet) in my app. Therefore I need to invalidate the session first followed by removal of element from dom. So the following is the final code:
On the client side:
logOutButton.addClickHandler(new ClickHandler(){
public void onClick(ClickEvent event) {
//Invalidate the session and then reload the application.
AsyncCallback<Void> callback = new InvalidateSessionHandler<Void>(SelectPersonView.this);
rpc.invalidateSession(callback);
}
});
class InvalidateSessionHandler<T> implements AsyncCallback<Void> {
SelectPersonView view;
public InvalidateSessionHandler(SelectPersonView view) {
this.view = view;
}
public void onFailure(Throwable ex) {
System.out.println("RPC call failed - InvalidateSessionHandler - Notify Administrator.");
Window.alert("Connection failed - please retry.");
}
public void onSuccess(Void result) {
//Reload the application.
Window.Location.assign("/");
}
}
On the server side:
public void invalidateSession() {
getThreadLocalRequest().getSession().invalidate(); // kill session
}
getThreadLocalRequest().getSession().invalidate(); returns me to my login window.
Window.Location.assign("/"); returns me to the tomcat page.
So use which ever suits you.
Asp.Net webform 4.5
I am referencing a script with a REV in a master page
<script src="<%# "/content/js/master.js?"+ RevID %>"></script>
RevID is a Public string in code behind.
This use to be in -head- section, and worked very well with
Page.Header.DataBind();
I now wish (as recommended) to move all scripts to the end of body.
when done, Page.Header.DataBind(); does not work anymore and I get src="".
Page.DataBind();
does work BUT it also re bind all control in child pages, so it is not a solution.
so how can I use
<%# ... %>
in the body section without
Page.DataBind();
?
As I have mentioned in the comment, if you want to use the code nuggets with # you will have to call DataBind method. Alternatively, if you have a public string field in code behind like this:-
public string RevID = "3";
Them you can simply access it like this:-
<script src="<%= "/content/js/master.js?"+ RevID %>"></script>
and this should work fine.
I want to remove mentioning scripts every page like
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.3/jquery-ui.min.js"></script>
it should be mentioned at once.like a common place to refer for source.
i have my framework in 3.5. how can i achieve this.
create a new project to hold all JavaScript files that you need want throughout the application. You can embed all the script into the DLL. That way, if the DLL is deployed to the website, all JavaScript files are also automatically deployed.
You can check out complete tutorial given below.
Managing-Your-JavaScript-Library-in-ASP-NET
You can do it by using master page. Demo like
<master page>
<header>
//add script file here
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.3/jquery-ui.min.js"></script>
//Other scripts
</header>
<content>
<Sub pages>
//Here you can debase your pages(like dynamically change pages), So the header script will referred from above header
</content>
<footer>
<//footer>
<master page>
MSDN link for Master page
If you don't want to use a master page you could use a server control and add it to the pages as needed.
Adding a Master Page is good.
As an alternate way, you can achieve this by creating a base page and adding scripts from web.config file into the page's header by overriding Page class OnInit event.
Create a class as BasePage inheriting from System.Web.UI.Page like
public class BasePage : System.Web.UI.Page
{
}
On all the pages where you want to load the scripts, inherit them from BasePage
Earlier
public partial class _Default : System.Web.UI.Page
{
}
Now
public partial class _Default : BasePage
{
}
Mention your script files at One place - Web.Config as below
<appSettings>
<add key="ScriptJquery" value="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"/>
<add key="ScriptJqueryUI" value="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"/>
</appSettings>
In the BasePage Class override OnInit as below
public class BasePage : System.Web.UI.Page
{
protected override void OnInit(EventArgs e)
{
IEnumerable<string> scripts = ConfigurationManager.AppSettings.Keys.OfType<string>().Where(s => s.StartsWith("Script"));
foreach (string script in scripts)
{
Literal scriptTag = new Literal();
scriptTag.Text = string.Format(#"<script src=""{0}"" type=""text/javascript""></script>",ConfigurationManager.AppSettings[script].ToString());
Page.Header.Controls.Add(scriptTag);
}
base.OnInit(e);
}
}
Are there any potential side-effects to below solution to injecting JavaScript from the content of an updated UpdatePanel?
The code for the UpdatePanel looks sort of like this:
<asp:UpdatePanel>
<asp:PlaceHolder ID="pnlScriptContent" Visible="false" runat="server">
<script id="script-content">
alert('Script was loaded correctly!');
</script>
</asp:PlaceHolder>
<asp:Button OnClick="ButtonClick" OnClientClick="LoadScript()" />
</asp:UpdatePanel>
The code-behind on the click of the Button shows the Panel pnlScriptContent.
protected void ButtonClick(object sender, EventArgs args)
{
pnlScriptContent.Visible = true;
}
The JavaScript-code looks like this:
var LoadScript = function() {
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(function (sender, args) {
eval($('#script-content').html());
};
}
I've seen other solutions to inject JavaScript after an UpdatePanel updates, but nothing that allows the injection of JavaScript inside script-tags in the content.
Is there a reason to this? Security maybe?
You can use RegisterClientScriptBlock static method of the ScriptManager class
I'm using Lightbox but i only want the references to the stylesheet and javascript files to be in the masterpage header on one page on the site (the page that uses lightbox). how do I programmatically add references to the stylesheet and javascript files in the page load?
the stylesheet is the 'css' folder and the three javascript files are a 'js' folder
try...
Page.ClientScript.RegisterClientScriptInclude("JScripts", ResolveUrl("~/js/JScripts.js"));
Add two placeholders ("JsPlaceholder" and "CSSPlaceholder") to your header on master page and call those methods:
public void AddJavascriptFile(string path)
{
PlaceHolder p = (PlaceHolder)Page.Header.FindControl("JsPlaceholder");
p.Controls.Add(new LiteralControl(string.Concat("<script type='text/javascript' src='", path, "'></script>\n")));
}
public void AddCssFile(string urlPath)
{
HtmlLink cssLink = new HtmlLink();
cssLink.Href = path;
cssLink.Attributes.Add("rel", "stylesheet");
cssLink.Attributes.Add("type", "text/css");
PlaceHolder p = (PlaceHolder)Page.Header.FindControl("CssPlaceholder");
p.Controls.Add(cssLink);
}
Try (in C# but you should get the idea):
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
HtmlLink cssLink = new HtmlLink();
//Create and configure the CSS link.
cssLink.Attributes.Add("rel", "Stylesheet");
cssLink.Attributes.Add("type", "text/css");
cssLink.Href = "~/Path/To/File.css";
//Add the CSS link to the page header.
this.Header.Controls.Add(cssLink);
//Add a script include to the page's ClientScript.
this.ClientScript.RegisterClientScriptInclude("NameOfScript", this.ResolveUrl("~/Path/To/File.js"));
}
you can do it like this
added this to your header:
<asp:placeholder runat="server" id="lightbox" visible="false">
<link rel="stylesheet" href="/css/style.css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"</script>
</asp:placeholder>
and from your codebehind set
lightbox.visible=true;
You should also note that normally you want to keep as much html on the page instead of having it in your codebehind so it will be easy for the designer to make changes