CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   JavaScript programming (http://www.codingforums.com/forumdisplay.php?f=2)
-   -   make document/ website viewable only when JavaScript is enabled (http://www.codingforums.com/showthread.php?t=252863)

Taro 02-28-2012 10:26 PM

make document/ website viewable only when JavaScript is enabled
 
Hello,

I would like to know how to make a website (in this case, a parent div) viewable only if JavaScript is enabled?

I tried to develop a code that can do this, but could not find the solution.
Code:

<html>
<head>
<script type="text/javascript">
window.onload=function()
var="true"
if(JavaScript.enabled)
{
document.getElementById('true')
}
OnClick()
{document.getElementById('toggle').onclick=function()
        {document.getElementById('jahava').style.display=(document.getElementById('jahava').style.display!='none')?'none':'';}}
</script>
</head>
<body>
<noscript>You must have JavaScript enabled to view the website.</noscript>
<div id="jahava">Contents Here</div>
</body>
</html>

:eek:

Old Pedant 02-28-2012 10:39 PM

Making it way too hard.

Code:

<html>
<body>
<div id="nojs">Sorry, you can't view this page if JavaScript isn't enabled</div>
<div id="contents" style="display: none;">
... your normal contents here ...
</div>
<script type="text/javascript">
document.getElementById("nojs").style.display = "none";
document.getElementById("contents").style.display = "block";
</script>
</body>
</html>

If JavaScript isn't available and enabled, then the "contents' will never get displayed. But the "nojs" will.

rnd me 02-29-2012 05:56 PM

i use css for this:

Code:

<html>
 <head>
  <style> .nojs #jahava {display:none;} </style>
 </head>
 <body class="nojs" onload='document.body.className="loaded" '>
  <noscript>You must have JavaScript enabled to view the website.</noscript>
  <div id="jahava">Contents Here</div>
 </body>
</html>


jalarie 03-02-2012 02:49 PM

Quote:

Originally Posted by Old Pedant (Post 1198718)
Making it way too hard.

Just to cover all bases, what if CSS is also disabled?

VIPStephan 03-02-2012 03:03 PM

Write the entire DOM with JavaScript. :rolleyes:

rnd me 03-02-2012 08:03 PM

Quote:

Originally Posted by jalarie (Post 1199838)
Just to cover all bases, what if CSS is also disabled?

fine.
Code:

<head>

<script type='text/javascript'>document.write("<st"+"yle>"); </script>       
<meta http-equiv="refresh" content="0;url=http://google.com/">
<style></style>

</head>

patent pending hack of mine...



or, use my crypto app, but hard-code the password by defining a PW string var of the password:
Code:

<html> <script>PW="defaultPassword"; eval( unescape( "function%20jcipher%28p%2Cs%29%7Bvar%20i%3D0%2CP%3D0%2CK%3D0%2Cb%3D%22%22%2CMax%3D0%2Cd%3D%5B%5D%3Bif%28p.slice%280%2C3%29%3D%3D%22zz%2C%22%29%7Bvar%20slen%3Ds.length+1%3Bd%3Dp.split%28%22%2C%22%29%3Bp%3D%22%22%3Bvar%20junk%3Dd.shift%28%29%2CScc%3DString.fromCharCode%3BMax%3Dd.length%3Bvar%20tr%3D%5BMax%5D%3Bfor%28var%20i%3D0%3Bi%3CMax%3Bi++%29%7BP%3Dd%5Bi%5D%3BK%3Ds.charCodeAt%28i%25slen%29%3Btr%5Bi%5D%3DScc%28P%5EK%29%3B%7Dreturn%20tr.join%28%22%22%29%3B%7Dreturn%20false%3B%7D%0A" ) );
var enc='zz,44 ,0,10,13,26,64,126,90,40,83,4,24,26,30,0,32,8,12,13,4,85,24,27,112,10,29,28,0,79,26,11,119,68,17,9,65,24,13,31,53,65 ,18,83,0,10,16,23,105,16,0,70,73,28,2,84,36,9,26,0,87,12,19,23,101,72,69,7,65,5,13,6,53,15,7,83,19,6,4,77,32,18 ,12,3,22,20,14,24,53,65,28,29,27,22,82,13,102,68,47,7,23,20,63,23,34,8,3,7,87,6,1,68,101,10,4,4,13,16,8,75,90,107 ,58,83,3,29,27,1,100,68,17,9,65,17,9,2,53,13,28,3,87,14,82,7,111,0,0,70,21,29,13,0,112,2,18,29,87,11,29,68,116,12 ,12,21,77,85,14,1,36,65,16,28,2,3,22,68,110,11,17,70,7,28,2,16,112,21,27,22,87,28,29,8,117,16,12,9,15,91'
 if (typeof PW == 'undefined'){var PW = prompt('Enter The Password for this Document:')};
 if (PW.length){  document.write( jcipher(enc, PW)); };
</script></html>

(changes from generated output in red). i made this a document to demo, but you can place that script bock into the parent div to conceal only part of a document.
this works with css, works with meta refresh blocking, and requires js to even read the source by any human.

felgall 03-02-2012 08:39 PM

There isn't really any purpose served by trying to obfuscate the code since the final HTML that it generates is easy to view regardless using either one of the development tools or a simple piece of JavaScript.

There also isn't really any reason for preventing those who have JavaScript disabled from accessing the page unless you are just too lazy to create a version of the page that will work for everyone. If you do want to drive those without JavaScript away from your page along with their friends and their friends then a simple "Go Away you are not welcome here" message in place of the content of your site will effectively produce the same end result (except slightly quicker).

It is unlikely that anyone with CSS disabled will have JavaScript enabled. It is far more likely that someone will override your CSS and/or JavaScript with their own.

You should only use document.write if you need to support Netscape 4 and earlier (although even by the time Netscape 4 was released it was considered extremely poor practice to have pages that don't work when JavaScript isn't available).

The only sites where it makes sense to have pages that actually rely on JavaScript to work are those on sites that are actually about JavaScript and anyone visiting a site about JavaScript with JavaScript disabled or not available in their browser at all wouldn't be expecting all the script examples to work in their browser.

Apart from animations, anything that can be done with JavaScript can also be done without it and the reason why a large number of people actually disable JavaScript is because they visit too many sites that have annoying animations.

Those who don't have JavaScript enabled can be divided into two groups - those who are using a browser without JavaScript who therefore can't turn it on and those who have deliberately disabled it for a reason and who therefore will NOT turn it on just because some site owner was too lazy to set up their site properly to work without it.

You may get away with a JavaScript only site if it is only intended for you and a few friends to use but you certainly can't get away with it for a business site (as one retailer discovered after being fined several million for not having a site that worked without JavaScript and which was therefore found to be discriminating against disabled people).

Philip M 03-03-2012 08:57 AM

:D
Quote:

Originally Posted by felgall (Post 1199978)

You may get away with a JavaScript only site if it is only intended for you and a few friends to use but you certainly can't get away with it for a business site (as one retailer discovered after being fined several million for not having a site that worked without JavaScript and which was therefore found to be discriminating against disabled people).

An urban myth, surely! If you can prove this, please supply a reference. I doubt that even in enlightened Australia it is a criminal offence to have a site which does not work without Javascript.

When I Google for "Javascript discrimination" I get many links that tell me that I must have Javascript enabled to view them! :) Including The Gay and Lesbian Rights Lobby. :D

Edit: Researching, I find that in 2000, an Australian blind man won a court case against the Sydney Organizing Committee of the Olympic Games (SOCOG). This was the first successful case under Disability Discrimination Act 1992 because SOCOG had failed to make their official website, Sydney Olympic Games, adequately accessible to blind users. Presumably he went on to sue book and newspaper publishers on the same grounds! :D I rather think that Javascript or lack of it was not an issue in this case. I suspect that he was not totally blind, but visually impaired, perhaps colour-blind.

IMHO people who deliberately disable Javascript in their browsers (a tiny minority surely?) must know perfectly well what they are doing, and should be prepared to accept the consequences. Same with those who insist on clinging to Netscape 4! If I disable Javascript in my browser that does not somehow make me disabled.

felgall 03-03-2012 10:10 PM

Quote:

Originally Posted by Philip M (Post 1200125)
An urban myth, surely

It was one of the big department stores in the USA sued by a blind person who was unable to use their site to make purchases because the site required JavaScript and the site was impossible to navigate with JavaScript enabled if you couldn't actually see the site. It was settled out of court for some undisclosed amount that is believed to be in the hundreds of thousands or millions. The store web site was also quickly changed so it would work for blind people so as to prevent a recurrence. It happened about two or three years ago.

The Gay and Lesbian Rights Lobby is not a business and so would not be subject to the rules regarding preventing people with disabilities from being allowed to buy products from them.

Studies have shown that over 70% of web users are disabled in some way although most of the disabilities are so minor that they can be compensated for by simple things such as zooming pages to make the text big enough to read or overriding the stylesheet with one that doesn't use the colours that the person cannot distinguish between.

Somewhere between 6 and 10% of web users have JavaScript disabled. In some cases it is because the browser or web reader being used doesn't support JavaScript. More common perhaps is all the people who have got fed up with ad after ad after ad after ad and silly animation after silly animation and who have therefore deliberately turned it off to get rid of all the garbage. The more knowledgeable of those have their browser set so that JavaScript is only enabled on those sites they regularly visit where the script actually serves a useful purpose (this the statistics show they have it disabled but they actually have it enabled for some sites).

You can get away with a site that doesn't work without JavaScript providing that you are not a business selling to the public. For a business there is the risk of being sued (assuming that the people without JavaScript along with their friends and their friend's friends don't simply switch to buying from the competition. For other sites requiring JavaScript simply means that you have instantly lost at least 6% of potential visitors and possibly several times that (depending on how many friends those who can't use the site have and how widely the message that the site is a heap of excrement actually spreads). After all for almost any web site there are dozens of alternative sites some of which will work without JavaScript that everyone can use instead of the one that is broken for some visitors.

MancunianMacca 03-03-2012 10:57 PM

This post is redundent as I read the above one :P How can I delete my posts?

rnd me 03-04-2012 12:42 AM

Quote:

Originally Posted by felgall (Post 1199978)
You may get away with a JavaScript only site if it is only intended for you and a few friends to use but you certainly can't get away with it for a business site (as one retailer discovered after being fined several million for not having a site that worked without JavaScript and which was therefore found to be discriminating against disabled people).

disabled people can run javascript. javascript greatly enhances accessibility and it's cousin usability. If a site is inaccessible, it's usually because of image text, plugins, and poor dom structure (lack of <Hx>s, <nav>, <img alt>, role=, or using <meta refresh=0>, etc).

now let me be clear: it is possible to reduce accessibility with javascript. many widgets are not keyboard-navigable as are navs that use jQuery().hover(), client-side redirects, harsh validation, and others gotchas.


That being said, as far as content goes, ajax injecting new static content is just as or more accessible than "regular" pages. <video> pumping captions to a div for screen-reader announcement is far more accessible than flash captions inside a black-box. ARIA form and control states provide a lot more info to AT than CSS, but they need javascript to be updated after page-load. Often, javascript is used to replace plug-ins with poor accessibility, and that's a clear win for accessibility.


Just like HTML, javascript is a tool that can build something accessible or inaccessible, it depends on how it's used. There is nothing intrinsically inaccessible about javascript itself.

webdev1958 03-04-2012 12:59 AM

Quote:

Originally Posted by felgall (Post 1199978)
You may get away with a JavaScript only site if it is only intended for you and a few friends to use but you certainly can't get away with it for a business site

Well, that is interesting because either:

1) You are telling a blatant lie.

or

2) You are displaying your incompetancy by not getting your facts straight first before posting

or

3) My legal adviser at the ATO has been lying to me when he informed me that I am under no legal obligation to provide full functionality to javascript disabled browsers and that I am not liable in any way to users with javascript disabled.

I seem to recall that you made similar comments/generalisations on another website. I have sent a copy of your posts, name and contact details to my legal adviser and asked him to investigate if what you say is true for my particular case and if not, to take what ever legal action he feels is appropriate.

VIPStephan 03-04-2012 02:16 AM

Look who’s chiming in! Long time, no see, bullant. ;)

Anyway, in response to Philip M’s opinion:
Quote:

Originally Posted by Philip M (Post 1200125)

IMHO people who deliberately disable Javascript in their browsers (a tiny minority surely?) must know perfectly well what they are doing, and should be prepared to accept the consequences. Same with those who insist on clinging to Netscape 4! If I disable Javascript in my browser that does not somehow make me disabled.

I have several minor websites registered with a free StatCounter account which is logging a maximum of 500 hits. On the currently most popular site of these, of the 500 hits it shows 9 without JavaScript. On the other sites it shows between 0 and 7 hits out of 500 without JS. I installed a new one recently with just 72 hits and there are 7 without JS.

OK, it might be very low in percentage, and hits are not equal to unique visitors (one visitor can make several hits, or several hits can come from one visitor only, respectively) but shows that it’s certainly possible that people browse without JS. I don’t see any reason to exclude them, especially when it’s not a lot of additional effort to provide proper accessibility.

webdev1958 03-04-2012 02:25 AM

[ot]
Quote:

Originally Posted by VIPStephan (Post 1200371)
I don’t see any reason to exclude them, especially when it’s not a lot of additional effort to provide proper accessibility.

Only if the client is willing to pay for the additional work. If they aren't, I am under no obligation, legal or otherwise, to provide it.
[/ot]

felgall 03-04-2012 06:53 AM

Quote:

Originally Posted by webdev1958 (Post 1200377)
[ot]


Only if the client is willing to pay for the additional work. If they aren't, I am under no obligation, legal or otherwise, to provide it.
[/ot]

If they are not prepared to pay the extra then why put the extra effort in to creating the JavaScript version. The version that works without JavaScript is always much easier to build - just not as quick and convenient for those with JavaScript enabled to use.

Only where the client is prepared to pay for the extra effort should you be creating the JavaScript version. There is no legal obligation to create a web page at all. There is a legal obligation in some countries for business sites to ensure their site is usable for ALL their visitors. Mostly it is a matter of wanting people to actually visit the site and so you build it so that it functions for all visitors because that's the best way to avoid getting a reputation for creating garbage web sites.

The only web site known to have been actually sued for it not working without JavaScript was the US store that settled out of court for a sum believed to be somewhere between 100,000 and 10,000,000. All other sites that require JavaScript usually just have everyone ignore that site and go to their competition instead. No one ever hears about them because no one bothers to visit them.

There are also lots of people writing garbage that they think is JavaScript that actually makes the site harder to use - that's why so many people disable JavaScript. If all the JavaScript were actually written properly then there'd be no reason for anyone to disable it. It takes several years for even an experienced programmer to learn how to write JavaScript properly so most of what is written is better suited to Netscape 2 than it is to modern browsers.

Just consider how much supposed JavaScript you see that contains any of the following long dead code:

language="javascript"
document.write
document.all
eval
alert
prompt
navigator.userAgent

any of those is a clear sign that the person who wrote the code has not learnt JavaScript but has just copied something that someone else wrote over 10 years ago.

Anyway, your legal advisor can't force people to visit crap web sites so their opinion is irrelevant.


All times are GMT +1. The time now is 05:03 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.