views:

605

answers:

9

I would like to know if anyone optimizes their web pages to have some friendly behavior if the user has Javascript disabled. Are there any tricks to provide some pseudo-scripting behavior in such instances? I'm talking basic things like opening links in new windows. I would imagine there is still a lot you can do without Javascript. It could be handy in some cases to have a page that has some features it can fall back on. Are there any practices?

Edit: good answers so far! Would people mind including some sample code (come on, HTML is sooo easy!) since I don't think I've seen a question like this on SO yet.

+3  A: 

You can use < noscript> tag to create hyperlink which target is _blank to open new window.

It's also good for SEO so that search engine like Google and Yahoo! can identify and crawl your links in your webpages.

< noscript> can also provide normal function to those js-disabled browser.

Billy
+7  A: 

What I sometimes do is set the features that require javscript to not display, then use javascript to make them display. That way people with out javascript wont see features they can't use. And then put alternative features in < noscript > tags.

John Isaacks
Care to tell me why you down-voted me?
John Isaacks
+4  A: 

Stackoverflow handles this in a nice way. The site is completely usable without Javascript, one can ask questions and answer them. Advanced features like voting don't work.

You have to find a compromise between the time you are willing to spend to make the site usable without Javascript and the amount of features that have to work.

Georg
+3  A: 

For opening links in new windows, you can use target="_blank"

<a href="http://www.google.com" target="_blank">Google</a>

Where I work, we use a landing page to ensure (to a certain degree) they have Javascript enabled before they get into the application...

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head>
    <meta http-equiv="content-type" content="text/html;charset=utf-8" />
    <title>Please Wait...</title>
  </head>
  <body>
    <h1>Please Wait....</h1>
    <noscript><h1>You need a JavaScript-enabled browser to use this application!</h1></noscript>
    <script type="text/javascript">
      <!--
        location.href = "FrontPage.aspx";
      // -->
    </script>
  </body>
</html>

Also, I am often amazed at how powerful CSS can be.

Josh Stodola
A: 

Here's a question (and another) that have lots of (differing) opinions on building a site that gracefully handles JavaScript being disabled.

Adam Neal
+2  A: 

When you use Javascript to open a window from a link, you can make it transparent so that it will fall back to the original behaviour of the link if Javascript is disabled:

<a href="http://www.guffa.com" target="_blank" onclick="window.open(this.href,this.target,'width=900,height=700');return false;">Guffa</a>

If Javascript is enabled, the onclick event will capture the click and open a window, then it returns false from the event so that the link is not followed. If Jasvascript is disabled, the link will be followed instead.

Guffa
+1 Some might question opening new windows, but your solution is the one I pick when it is desired/required. Just be aware the user is still in control of their browser and may suppress any new window, instead opening the URL in a new tab or even the same window.
Grant Wagner
+2  A: 

"I would imagine there is still a lot you can do without Javascript" - funny, not so long ago we did everything without javascript.

The best approach is what's known as 'progressive enhancement'. The principle is to make something that works without JavaScript, and then 'layer' on additional behaviours by binding the JavaScript to the objects. Libraries such as jQuery make this straightforward, as they support CSS-style selectors. That way you can apply JS behaviours almost the same way you would apply CSS (OK, a slight exaggeration).

Some thought needs to be taken to think about what's a good baseline and how pages should be changed.

Often the real extra work is at the server-side, where you may have to have multiple request processing systems. It's a bit of a big field to address in one little response!

Also, it's worth thinking about who and why users are not using javascript. Here's something I wrote a while ago on the subject should you be interested.

An example? Take something simple, eg. you want to display nice 'tooltips' by form fields when the user mouses to them. In the HTML, these could (for example) be marked up as paragraphs:

<label for="username">User name</label><input type="text" id="username" />
<p>Username must be between 6-8 characters</p>

For non JS users, the prompt can simply be displayed as a nice paragraph. So you style this with CSS. Have additional CSS for JS users, which hides the paragraph by default. So: 2 sets of CSS, one overrides the other.

non JS:

form p {
    margin: 10px 0 0 0;
    border-bottom: 1px solid grey;
}

js:

form p {
    position: absolute; display: none;
    width: 180px;
    background-image: url(nice-bubble.gif);
    padding: 10px;
}

How you apply CSS for JS/non JS situations is up to you. There are numerous options. Personally I like to code for JS in the CSS and have a noscript.css variant (i.e. working backwards) in a tag. Which isn't valid XHTML, but works nicely.

Then, have some JS which looks for the elements and manages the display code for the tooltips. eg:

$(document).ready(function() {
    $('form input').focus(function() {...display paragraph...});
    // etc;
})

That's bogus code, but you get the picture. What you end up with is 1 set of HTML, with 2 presentations, and additional behaviours, and no JS tangled up in your HTML itself.

mdja
+1  A: 

Folks disabling Javascript might also trim CSS, Java, and other "presentation" functionality to save processing power on a lower-powered browser. (For example, a web phone or older model computer.) You could mitigate that somewhat by adding simple functionality to the server, like they did back in the days of CGI (random intro here). In that case, your server could redirect non-JS users to a GET/POST page version. I'd also make the responses as simple and unadorned as possible, since you might output to a tiny screen.

Matthew Glidden
A: 

jQuery really helps. A lot of the time with big JavaScript plugins, (tabs, tooltips, etc...) you write your markup in a very semantic way.

Example: The tabs plug-in requires that you create anchored divs, i.e.

<a href="#Preferences">Preferences</a>
<a href="#Tools">Tools</a>
<div id="Preferences">
   blah
</div>
<div id="Tools">
   bar
</div>

This makes sense semantically, and without the tabs plug-in, you get some functionality. When you have javascript and jquery-tabs enabled, though, you get a really nice, rich interface.

This is really just progressive enhancement, as some others have noted. Write semantic markup, enhance it with CSS, then enhance it with Javascript. You should end up with something that works for everybody, even lynx users.

Stuart Branham