Asp.Net - 检测到页面上没有 javascript? (已更名标题)

发布于 2024-11-03 11:06:54 字数 437 浏览 1 评论 0原文

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

我可以添加 来显示所有重要内容,但空白 TabContainer 仍会呈现。

我想在标题中添加 a 以重定向到同一页面加上 ?noscript=true ,但它不应该是无限的。我认为使用 PlaceHolder 控件会在当前 url 不存在时放置适当的 具有 noscript 查询值。

然后,当 noscript 查询值存在时,我可以将 TabContainer 的 Visible 属性设置为 false。

这是正确的做法吗?

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.

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

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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

攀登最高峰 2024-11-10 11:06:54

您可以使用 HttpBrowserCapabilitites 类来获取有关浏览器的信息,用于检查 JavaScript 支持的属性称为 EcmaScript 版本。如果版本 >= 1,则浏览器支持 JavaScript。

You can use HttpBrowserCapabilitites class to obtain information about the browser, the property to check for JavaScript support is called EcmaScriptVersion. If it has a version >= 1, the browser supports JavaScript.

素手挽清风 2024-11-10 11:06:54

我想出了一种可行的方法,但对于 Asp.Net 来说仍然很新,所以我对其他人的想法很感兴趣。

编辑我通过引入一个临时(仅限会话)cookie 来扩展这个想法,该 cookie 会记住浏览器是否有 NoScript,因此我们不必继续重定向到同一页面,我们只需使用下次请求页面时该 bool 值。

我在母版页的头部执行了此操作:

<noscript>
  <%# NoScriptPlaceHolder %>
</noscript>

在 TabContainer 的 Visible="<%# !NoJavascript %>" 属性中。

那么在CodeBehind中:

protected bool NoJavascript
{
    get 
    {
        TempCookie cookie = new TempCookie(); // session only cookie
        if (cookie.NoJavascript) return true;
        bool noJavascript = !string.IsNullOrEmpty(Request["noscript"]);
        if (noJavascript)
            cookie.NoJavascript = noJavascript;
        return noJavascript;
    }
}

protected string NoScriptPlaceHolder
{
    get
    {
        if (NoJavascript)
            return string.Empty;
        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);
        return meta;
    }
}

还有其他想法吗?

未来的想法:如果他们没有启用 Cookie,我想将 noscript 查询值传递给每个请求的实用程序也会有所帮助,否则他们将在每个请求上不断重定向到同一页面。

I came up with a method that works, still pretty new to Asp.Net so I'm interested in what others think.

Edit: I expanded on the idea by introducing a temporary (session only) cookie that remembers if the browser has NoScript so we don't have to keep redirecting to the same page, we just use that bool value the next time a page is request.

I did this in the head of the master page:

<noscript>
  <%# NoScriptPlaceHolder %>
</noscript>

In the TabContainer's Visible="<%# !NoJavascript %>" property.

Then in the CodeBehind:

protected bool NoJavascript
{
    get 
    {
        TempCookie cookie = new TempCookie(); // session only cookie
        if (cookie.NoJavascript) return true;
        bool noJavascript = !string.IsNullOrEmpty(Request["noscript"]);
        if (noJavascript)
            cookie.NoJavascript = noJavascript;
        return noJavascript;
    }
}

protected string NoScriptPlaceHolder
{
    get
    {
        if (NoJavascript)
            return string.Empty;
        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);
        return meta;
    }
}

Any other ideas?

Future thought: If they don't have Cookies enabled, I guess a utility to pass the noscript query value to each request would help as well, otherwise they will get continually redirected to the same page on each request.

焚却相思 2024-11-10 11:06:54

好吧,因为我很彻底,并且不想重复代码,所以我创建了这个组件来执行我的其他答案,并检查会话和视图状态以进行先前的检测。

该组件的价值在于它可以在其他页面上使用,并且可以访问该组件在其他页面上使用的相同会话/cookie 值。

有什么改进建议吗?

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>");
        }
    }
}
}

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.

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.

Any suggestions at improvement?

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>");
        }
    }
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文