Redirect if JavaScript is not enabled [duplicate] - c#

This question already has answers here:
How to detect if JavaScript is disabled?
(39 answers)
Closed 5 years ago.
I am working on a logic where I want user to be redirected to a particular page if JavaScript is not enabled. I have put a meta-tag which will refresh every few seconds and if JavaScript is enabled and I want to use JavaScript to remove that element.
I have tried many things but I have been unsuccessful in removing the tag. Also I tried to empty the content of the tag but it still redirects.
Is there any other way to deal with this issue?
I am posting some code for you to take a look, I just grabbed it from the web but seems to be emptying the contents when I debug through the code:
var m = $('meta');
for (var c = 0; c < m.length; c++) {
m[c].parentNode.removeChild(m[c]);
m[c].content = '';
}
I am also open to a server side solution but the client does not send much information such as if javaScript is enabled or not.......

Try this:
// THIS DOES NOT WORK
$( 'meta[http-equiv="refresh"]' ).remove();
It certainly depends on how soon your jQuery code executes and how soon your refresh triggers...
Update: The above method does not work. Even though the meta element is removed from the DOM, the browser still executes the refresh.
Live demo: http://www.ecmazing.com/misc/test-removing-meta-element/
I believe you should also be able to do this:
<noscript>
<meta http-equiv="refresh" content="...">
</noscript>
So, if you wrap your meta element in a NOSCRIPT element, it should only be parsed if JavaScript is disabled.
Read about the NOSCRIPT element here: https://developer.mozilla.org/en/HTML/Element/noscript

Related

Removing a specific query from url using jquery [duplicate]

This question already has answers here:
How can I delete a query string parameter in JavaScript?
(27 answers)
Closed 9 years ago.
Good morning guys, i have encountered a problem here.
This is my current URL:
blabla/shoe?type=boot&zoom=true
i want to remove the "&zoom=true" part, without reloading the page.
But bare in mind that the URL may also look like this:
blabla/shoe?zoom=true
how do i accomplish this, can someone give me an example of Jquery statements. So can start from there. thank u in advance
The reason why i wanna do this is because of the following:
<div class="standard" style="<%=isZoom?"":"display:none;" %>">
isZoom() is checking for any zoom query in the URL, i want the &zoom=true to go away so that the div can be shown
Changing the URL in the browser will reload the page. What's more, the loaded page will not contain your ASP.NET condition. It will only say style="display: none;", so even if you could change the URL without reloading the page, you'd still need to reload the page to have ASP.NET render something else.
The good news, though, is that you can show a div without changing the URL or reloading the page:
$('.standard').show();

Load a page with pagemethods inside a div tag inside a MasterPage

I have a MasterPage which will appear in every page in the application and I'm trying to load a "LoginBox" which uses PageMethods inside a Div tag in this MasterPage
So far I have tried doing as I would do on a Content Page, tried converting it into a User Control and tried using a server side include (< !--#include file="LoginBox.aspx"-->)
None succeeded.
I can see with firebug that the webresources get loaded but the PageMethods javascript isn't created in any of those methods.
I am REALLY trying to avoid having to create a WebService for this, and moving the LoginBox is not an option, I would rather drop the MasterPage idea, but then maintenance would become hell.
I need ideas or a direction on this.
Any help is appreciated
I got it working successfully with an iframe loaded from javascript, to me it's an ugly solution, but still one. I'm open for better solutions
<script type="text/javascript">
(function () {
var e = document.createElement('iframe');
e.setAttribute("src", "LoginBox.aspx");
e.setAttribute("scrolling", "no");
e.setAttribute("frameborder", "0");
e.setAttribute("height", "73px");
e.setAttribute("width", "225px");
e.setAttribute("marginheight", "0px");
e.setAttribute("marginwidth", "0px");
e.async = true;
document.getElementById('loginboxd').appendChild(e);
} ());
</script>
Looks to me like you're mashing classic asp with ASP.NET
the point of user controls is to encapsulate exactly what you are doing here.
even then however you will find your attempts to componentize your code will still lead to a messy mess mess. consider moving over to ASP.NET MVC if you can. with that you can do far more suitable and cleaner things to keep your codebase clean.

How can I read CSS properties in ASP.NET? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Is there a CSS parser for C#?
I'd like to access some CSS properties of my website defined in an external .css file at runtime. I've found that there is a way to programmatically set css properties in the codebehind, but I haven't come across any way to read the ones that are already defined. (I'm using C#)
You can read them "at run time" only using client side script.
With jQuery it's really simple, plus you can then send the value to the server using AJAX then handle it or store it for later use.
If it's valid option let me know and I can post basic example of what I mean.
Basic example
First, the HTML used:
<div class="mydiv" id="testDiv">I'm red</div>
<button type="button" id="testButton">Test</button>
CSS:
<style type="text/css">
.mydiv { background-color: red; }
</style>
Now you have to include jQuery:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
And finally have this JS code:
<script type="text/javascript">
$(document).ready(function() {
$("#testButton").click(function() {
var oDiv = $("#testDiv");
var sColor = oDiv.css("background-color");
$.get("TestZone.aspx?cssvalue=" + sColor);
alert("value has been sent");
});
});
</script>
This will send the runtime background color of the test div to the server when the button is clicked, and in the code behind of TestZone.aspx you can handle the value. The example send it over the querystring you can same way send it as POST data if you prefer.
This is the way to get the display value for aWebControl in the code behind:
aWebControl.Style["display"]
ASP.NET only deals with the markup of the page itself.
You can programmatically get or set inline styles (CSS declared inline with an element using the style attribute) using the WebControl.Style property. This gives you a collection of name/value pairs that represents the inline style for that element.
control.Style["font-family"] = "Verdana";
Inline styles are considered pretty unpleasant, so don't use them unless you have a really good reason.
Anything outside of the page, you cannot access with ASP.NET natively. Essentially, the external stylesheets are only loaded by the browser rendering the page and aren't handled by the ASP.NET runtime, so you don't get a chance to inspect them.
If you want to interpret those files, you will need to load them manually and parse them with a CSS parser. Have a look at this question for possible solutions:
Is there a CSS parser for C#?

Response.AddHeader in an ascx file

Is it possible to use the Response.AddHeader particularly Im trying to use the "Refresh" of it. I need it to pause before it redirects but the place where the code is being ran is in a ASCX in the codebehind. It does nothing when I have the following in my Codebehind:
HttpContext.Current.Response.AddHeader("Refresh", "6;URL=Default.aspx");
Any way I can redirect a user in the codebehind on a ascx page?
I don't know why that isn't working for you - it seems to be the right syntax to add the redirect to your HTTP header, and it works for me on several browsers. To debug this, you can run Fiddler or a similar tool to see the full HTTP response, and see if it's not making it to the header.
You may want to try reformatting slightly - the examples I've seen have a space between the semi-colon and the url, and url keyword in lower-case. I doubt this is the problem, but worth a try, if it's failing on specific browsers:
Refresh: 0; url=http://www.example.com/
An alternative approach, maybe easier to debug, is to use a meta tag instead of HTTP header, so it actually shows up in the markup. Something like:
var metaControl = new HtmlMeta
{
Content = "2;url=http://webdesign.about.com/",
HttpEquiv = "refresh"
};
Page.Header.Controls.Add(metaControl);
This will add a <meta> tag to your <head> section, which should have the desired effect.
Have you tried
if(today == "Friday")
{
Response.Redirect("destination.aspx")
}
If that doesn't work, please be more specific about what you are trying to accomplish.

Why does it make a difference where I include the jQuery script file?

On my master page (for all pages in my site) I have a ToolkitScriptManager.
On my content page, there are a series of hyperlinks and divs for collapsible functionality.
The code to show/hide the panels work like the following:
$(document).ready(function() {
// Hookup event handlers and execute HTML DOM-related code
$('#nameHyperLink').click(function() {
var div = $('#nameDiv');
var link = $('#nameHyperLink');
if (div.css('display') == 'none') {
link.text('Hide Data');
div.show('100');
}
else {
link.text('Show Data');
div.hide('100');
}
});
});
If I include a ScriptReference to the jQuery 1.4.2 file in the toolkitscriptmanager, the javascript code is executed incorrectly on the page (only the text for the hyperlink is changed, the div is not actually shown.) However, if I don't include the jQuery file in the ToolkitScriptManager and instead include it in the content page, it works correctly.
I'm a Javascript/jQuery newbie, and this makes no sense at all. What's going on here?
Positioning of the script include is important for the jQuery ref. If you look at your generated source I would bet the tag is below the script function(). You should make sure that the jQuery reference comes as early as you can get it in the page source.
Try moving the jQuery library reference into the head of your master page, that should work. Otherwise post up some source!
Like Tj says... should probably be in the head section of your master page. Also, it's nice to link to Google's version of this library, because chances are your users will already have it cached. For instance, look at the source for this very page.
The two most probable causes here are $ not being defined yet (see Tj's answer) and $ getting defined by another library, such as prototype.
I would highly suggest you look into using Firebug's javascript debugger, or at least take a look at Firefox's built in error console (Tools -> Error console). That will give you a much better clue what is going on other than "it's not working."

Categories