Asp.Net - 检测页面上没有JavaScript的? (改名为标题) [英] Asp.Net - Detect no javascript on page? (renamed title)

查看:135
本文介绍了Asp.Net - 检测页面上没有JavaScript的? (改名为标题)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个显示其在TabContainer的所有内容的页面,但如果JavaScript在浏览器中禁用它只是显示一个空白页。

I have a page that displays all its content in a TabContainer, but if javascript is disabled on the browser it just displays a blank page.

我可以添加< NOSCRIPT方式> 来显示所有的重要内容,但空白TabContainer的仍呈现

I can add a <noscript> to display all the important content, but the blank TabContainer still renders.

我想在标题中增加一个重定向到同一页面加?无脚本=真实的,但它不应该是无限的。我想用一个占位符的控制,将出台相应的&LT; META HTTP-EQUIV =刷新CONTENT =0; URL =网址无脚本=真正的&GT; 时当前URL没有无脚本查询值。

I'd like to add a in the header to redirect to the same page plus ?noscript=true, but it shouldn't be infinite. I figure using a PlaceHolder control that will put the appropriate <META HTTP-EQUIV="Refresh" CONTENT="0; URL=url?noscript=true"> when the current url doesn't have the noscript query value.

然后,我可以将Visible属性设置为false时无脚本查询值是present的TabContainer的。

Then I can set the Visible property to false for the TabContainer when the noscript query value is present.

这是去了解它的正确方法?

Is this the right way to go about it?

推荐答案

好了,因为我是彻底的,不希望重复code,我创造了这个组件,做我的其他答案加检查会议,并视图状态为previous检测。

Well, because I'm thorough, and don't want to duplicate code, I created this component that does my other answer plus checks session and viewstate for previous detection.

组件的价值在于,它可以在其他网页被使用,并且将可被上与成分的其他网页所使用的相同的会话/ cookie值

The value of the component is that it can be used on other pages and it will have access to the same session/cookie value that was used on other pages with the component.

在改进建议?

using System;
using System.ComponentModel;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace AspNetLib.Controls
{
/*
 * This component should be placed in the <head> html tag and not in the body or form.
 */
[DefaultProperty("Noscript")]
[ToolboxData("<{0}:NoJavascript runat=server />")]
public class NoJavascript : WebControl
{
    HttpRequest Request = HttpContext.Current.Request;
    HttpSessionState Session = HttpContext.Current.Session;
    HttpResponse Response = HttpContext.Current.Response;

    [Bindable(true)]
    [Category("Appearance")]
    [DefaultValue(false)]
    [Localizable(true)]
    public bool Noscript
    {
        get
        {
            bool noscript;
            // check if we've detected no script before
            try
            {
                noscript = Convert.ToBoolean(Session["js:noscript"] ?? false);
                if (noscript) return true;
            }
            catch { } // if session disabled, catch its error
            HttpCookie Cookie = Request.Cookies["JavascriptDetectionComponent"];
            if (null != Cookie)
            {
                noscript = !string.IsNullOrEmpty(Cookie["js:noscript"]);
                if (noscript) return true;
            }
            noscript = Convert.ToBoolean(ViewState["js:noscript"] ?? false);
            if (noscript) return true;

            // if we've returned from meta evaluate noscript query setting
            noscript = !string.IsNullOrEmpty(Request["noscript"]);
            if (noscript)
            {
                SetNoScript();
                return true;
            }
            return false;
        }
    }

    [Bindable(true)]
    [Category("Misc")]
    [DefaultValue("")]
    [Localizable(true)]
    public string CookieDomain
    {
        get { return Convert.ToString(ViewState["CookieDomain"] ?? string.Empty); }
        set { ViewState["CookieDomain"] = value; }
    }

    private void SetNoScript()
    {
        try { Session["js:noscript"] = true; }
        catch { }// if session disabled, catch its error
        ViewState["js:noscript"] = true;
        HttpCookie Cookie = new HttpCookie("JavascriptDetectionComponent");
        if (!string.IsNullOrEmpty(CookieDomain))
            Cookie.Domain = CookieDomain;
        Cookie["js:noscript"] = "true";
        Response.Cookies.Add(Cookie);
    }

    protected override void RenderContents(HtmlTextWriter output)
    {
        if (!Noscript)
        {
            string url = Request.Url.ToString();
            string adv = "?";
            if (url.Contains('?'))
                adv = "&";
            string meta = string.Format("<META HTTP-EQUIV=\"Refresh\" CONTENT=\"0; URL={0}{1}noscript=true\">",
                url, adv);
            output.WriteLine("<noscript>");
            output.WriteLine("  " + meta);
            output.WriteLine("</noscript>");
        }
    }
}
}

这篇关于Asp.Net - 检测页面上没有JavaScript的? (改名为标题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆