0

I've an aspx page which loads several images after detecting client's screen size. Here is how I do this: In jquery code, I first detect user screen size, then depending on the size, I do some calculations to decide the number of images to be fetched from database. Then with the use of jquery ajax, I call up a web service which returns json response. Code picks up json and creates page.

Now, if user has disabled javascript, this approach doesn't work. Now I have to create page using c# code on code behind. The problem is, I can't detect if javascript is disabled from code behind (I think this is not possible). I do not want to display a button at the top says something "Javascript is disable in your browser. Please click here to display this page". I can put this button in noscript tags. But I do not want user have to click a button before seeing images on page.

What I want, if javascript is disabled, system should detect it and immediately run a function from code behind. (IN this case page size would be fixed and won't vary according to user screen) I can not use a hidden field or cookie to detect it since they both will need a postback before detecting javascript is diabled. (We know that js code can't run before any page life-cycle event).

Well I don't have much hope that my problem could be resolved, but still I want to give it a try. May be someone already have a solution. It would be extremely helpful if you could give your views or suggestion to change/update logic.

Any help would be greatly appreciated.

Thanks and Regards Praveen

Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
Praveen Tiwari
  • 1,200
  • 1
  • 12
  • 25
  • stop worrying about users with disabled javascript, it was a problem before when it was new, now javascript is a must to surf over the web. Summary: It's their problem that they have disabled it. – Matija Grcic Oct 12 '12 at 10:02
  • @Rawling, this is not a duplicate of that post, since I've surfed too much before asking. I never wanted a redirect solution. I thought, there could be a clean way. I also wrote- "Well I don't have much hope that my problem could be resolved". Anyways thanks. – Praveen Tiwari Oct 12 '12 at 10:34
  • You're going to need a redirect in there somewhere. The server doesn't know whether the client supports Javascript; you'll need to get the client to tell the server at some point after the page is sent i.e. redirect. The linked question provides a way to automatically redirect to a no-code version of the page; one of the answers provides a way to automatically recdirect to a code-enabled version. – Rawling Oct 12 '12 at 10:44

2 Answers2

1

1.) Use JavaScript to set a cookie, and then test for that cookie using server-side scripting upon subsequent page views; deliver content appropriately.

How to detect if JavaScript is disabled?

2.) You can use <noscript> tag.

The <noscript> tag: can be used to provide an alternate content for users that have disabled scripts in their browser or have a browser that doesn’t support client-side scripting.

<noscript>
    ....
</noscript>

3.) Detect if JavaScript is enabled in ASPX

protected void Page_Load(object sender, EventArgs e)
{
    if (Session["JSChecked"] == null)
    //JSChecked -indicates if it tried to run the javascript version
    {
        // prevent infinite loop
        Session["JSChecked"] = "Checked";
        string path = Request.Url + "?JScript=1";
        Page.ClientScript.RegisterStartupScript(this.GetType(), "redirect", 
          "window.location.href='" + path + "';", true);
    }
    if (Request.QueryString["JScript"] == null)
        Response.Write("JavaScript is not enabled.");
    else
        Response.Write("JavaScript is enabled.");
}
Community
  • 1
  • 1
Kapil Khandelwal
  • 15,958
  • 2
  • 45
  • 52
  • Thanks for responding. I already said I can't use cookies since they will be set by javascript which executes after page loads. redirects would not suit my situation. I was looking for a solution and I come to know what I was thinking is not possible. – Praveen Tiwari Oct 12 '12 at 10:30
0

Make default version without javascript and redirect to javascript version if javascript enabled. How to redirect if javaScript is disabled?

Community
  • 1
  • 1
Frank59
  • 3,141
  • 4
  • 31
  • 53
  • Thanks for responding. Redirects would not suit my situation. I was looking for a solution and I come to know what I was thinking is not possible. – Praveen Tiwari Oct 12 '12 at 10:31