0

I have a web page where a part of the page is loaded via AJAX, but I also wanted to make it work if JavaScript is not enabled.

so, i wrote the code as

<script>
    fetchCitiesAndDisplay ();
</script>
<noscript>
    <%
        out.print(Cities.getHtml ());
    %>
</noscript>

where

fetchCitiesAndDisplay ()

is a javaScript Method, which will get the content (executes the code in Cities.java) and displays it.

Cities.java

is a Java class which will collect the cities from database and generates the html content to be displayed.

When JavaScript is off, everything works great.. as the ajax call will not be executed and only Cities.getHtml () will be called once.

but, when JavaScript is enabled, The Ajax call is also executed and Cities.getHtml () is also called.. even-though the display looks right because of tag, Cities.java will get the call twice for the same data, and it eats up a lot of time...

How to solve this?

amithgc
  • 6,167
  • 6
  • 29
  • 38

3 Answers3

1

I'm wondering if maybe you don't understand how <noscript> works. It doesn't come into play in your server-side template so whatever server-side logic you put in there will ALWAYS execute whether javascript is ultimately enabled or not in the browser.

Therefore, this doesn't make a whole lot of sense to me because the HTML inside <noscript> is always going to be generated whether javascript is on or off. It will always be in the page. So, if it's always in the page, then why fetch it via ajax when javascript is on?

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • My Primary objective is fetch it via AJAX, to make the webpage render faster... but if JS is disabled, only then i want to get and display it by directly calling out.print(Cities.getHtml ()); – amithgc Jan 10 '12 at 01:57
  • What you have is NOT doing that. It is ALWAYS calling the server-side code in Cities.getHTML(). Just look at your generated page with view/source when javascript is one and you will see the full HTML there inside the ` – jfriend00 Jan 10 '12 at 01:59
  • will be hidden automatically when JS is disabled, my problem is... how to stop the call being made when JS is disabled.. – amithgc Jan 10 '12 at 02:02
  • How would you do it? say u have a java class which will give you the html content to display.. and you want to get the data from it via AJAX when JS is enabled.. and how would u get the data when JS is disabled? – amithgc Jan 10 '12 at 02:05
  • I'm confuzzled @jfriend00. It sounds like you're suggesting that the ` – fireshadow52 Jan 10 '12 at 02:28
  • 1
    A simple [Google search](https://www.google.com/search?rlz=1C1CHFX_enUS437US437&sourceid=chrome&ie=UTF-8&q=how+to+detect+on+server+that+javascript+is+disabled) gives you education on the options. Basically, you can't detect on server-side if javascript is disabled in one page request. Is it really worth building two versions of your site to server the tiny percent of people without JS enabled? – jfriend00 Jan 10 '12 at 02:29
  • 1
    @fireshadow52 - the way the OP is using ` – jfriend00 Jan 10 '12 at 02:32
  • @AmithGC - ultimately you probably want to get a cookie set that indicates that this browser should get the no-javascript rendering. How you get that cookie initially set depends upon the flow/design of your site. Since you can't use javascript to set the cookie, you will probably have to post a form to the server and have the server set the cookie and then the server will know to render to this browser in the non-javascript-way. – jfriend00 Jan 10 '12 at 02:35
  • The only reason i want to get the data when JS is disabled is for the Search engine crawlers.... – amithgc Jan 10 '12 at 04:24
  • @AmithGC - then maybe you should sniff the user-agent of the search engine crawlers on your server and serve up the content for them that way (don't even put it in ` – jfriend00 Jan 10 '12 at 04:28
  • thanks a lot, will try the cookie approach, will update the thread once im done... – amithgc Jan 10 '12 at 04:41
0

i have to agree with @jfriend00, its not possible to identify if javascript is enabled using 1 page

You can use javascript to set a hidden variable with some value, when the page submits you can check for it and identify if javascript is enabled or not.. but this requires 1 request/response cycle, may be you can have an interim page which forwards the request to the intended page (with the hidden variable i discussed above).

Anantha Sharma
  • 9,920
  • 4
  • 33
  • 35
0

Why not have your javascript code load a a tiny html/image/etc snippet on page load. If javascript is enabled, you can see that file was requested. If it's disabled, there will be no request for that file.

LazyCubicleMonkey
  • 1,213
  • 10
  • 17