Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-28-2012, 10:26 PM   PM User | #1
Taro
Regular Coder

 
Taro's Avatar
 
Join Date: Oct 2011
Location: Geraldton, Ontario
Posts: 155
Thanks: 1
Thanked 1 Time in 1 Post
Taro is an unknown quantity at this point
Post 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>
__________________
Element ID

Webs Support Helper

Your friendly neighborhood Taroman.
Taro is offline   Reply With Quote
Old 02-28-2012, 10:39 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,248
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
defencedog (03-04-2012)
Old 02-29-2012, 05:56 PM   PM User | #3
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,469
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
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>
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%

Last edited by rnd me; 02-29-2012 at 05:58 PM..
rnd me is offline   Reply With Quote
Old 03-02-2012, 02:49 PM   PM User | #4
jalarie
Regular Coder

 
Join Date: Jun 2002
Location: Flint, Michigan, USA
Posts: 593
Thanks: 1
Thanked 19 Times in 19 Posts
jalarie is an unknown quantity at this point
Quote:
Originally Posted by Old Pedant View Post
Making it way too hard.
Just to cover all bases, what if CSS is also disabled?
__________________
Visit my site at http://spruce.flint.umich.edu/~jalarie/.
jalarie is offline   Reply With Quote
Old 03-02-2012, 03:03 PM   PM User | #5
VIPStephan
The fat guy next door


 
VIPStephan's Avatar
 
Join Date: Jan 2006
Location: Halle (Saale), Germany
Posts: 7,614
Thanks: 5
Thanked 865 Times in 842 Posts
VIPStephan is a jewel in the roughVIPStephan is a jewel in the roughVIPStephan is a jewel in the rough
Write the entire DOM with JavaScript.
__________________
Don’t click this link!
VIPStephan is offline   Reply With Quote
Old 03-02-2012, 08:03 PM   PM User | #6
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,469
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
Quote:
Originally Posted by jalarie View Post
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.
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%

Last edited by rnd me; 03-02-2012 at 08:14 PM..
rnd me is offline   Reply With Quote
Old 03-02-2012, 08:39 PM   PM User | #7
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,465
Thanks: 0
Thanked 499 Times in 491 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
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).
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 03-03-2012, 08:57 AM   PM User | #8
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by felgall View Post

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.

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! 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.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 03-03-2012 at 11:14 AM..
Philip M is offline   Reply With Quote
Old 03-03-2012, 10:10 PM   PM User | #9
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,465
Thanks: 0
Thanked 499 Times in 491 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
Quote:
Originally Posted by Philip M View Post
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.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 03-03-2012, 10:57 PM   PM User | #10
MancunianMacca
New Coder

 
Join Date: Feb 2012
Location: England
Posts: 59
Thanks: 12
Thanked 2 Times in 2 Posts
MancunianMacca is an unknown quantity at this point
This post is redundent as I read the above one :P How can I delete my posts?
MancunianMacca is offline   Reply With Quote
Old 03-04-2012, 12:42 AM   PM User | #11
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,469
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
Quote:
Originally Posted by felgall View Post
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.
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%
rnd me is offline   Reply With Quote
Old 03-04-2012, 12:59 AM   PM User | #12
webdev1958
Banned

 
Join Date: Apr 2011
Posts: 656
Thanks: 14
Thanked 69 Times in 69 Posts
webdev1958 can only hope to improve
Quote:
Originally Posted by felgall View Post
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.

Last edited by webdev1958; 03-04-2012 at 02:28 AM..
webdev1958 is offline   Reply With Quote
Old 03-04-2012, 02:16 AM   PM User | #13
VIPStephan
The fat guy next door


 
VIPStephan's Avatar
 
Join Date: Jan 2006
Location: Halle (Saale), Germany
Posts: 7,614
Thanks: 5
Thanked 865 Times in 842 Posts
VIPStephan is a jewel in the roughVIPStephan is a jewel in the roughVIPStephan is a jewel in the rough
Look who’s chiming in! Long time, no see, bullant.

Anyway, in response to Philip M’s opinion:
Quote:
Originally Posted by Philip M View Post

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.
__________________
Don’t click this link!
VIPStephan is offline   Reply With Quote
Old 03-04-2012, 02:25 AM   PM User | #14
webdev1958
Banned

 
Join Date: Apr 2011
Posts: 656
Thanks: 14
Thanked 69 Times in 69 Posts
webdev1958 can only hope to improve
[ot]
Quote:
Originally Posted by VIPStephan View Post
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]
webdev1958 is offline   Reply With Quote
Old 03-04-2012, 06:53 AM   PM User | #15
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,465
Thanks: 0
Thanked 499 Times in 491 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
Quote:
Originally Posted by webdev1958 View Post
[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.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/

Last edited by felgall; 03-04-2012 at 07:10 AM..
felgall is offline   Reply With Quote
Reply

Bookmarks

Tags
document, element, jahava, script

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:51 PM.


Advertisement
Log in to turn off these ads.