0

Possible Duplicate:
How to detect if JavaScript is disabled?

I need to detect whether javascript is disabled in browser.

To do so I have put an iframe inside the document and refreshing the page in a interval of 10sec. Once script is disabled, it has to redirect the top parent with javascript error URL. Here is my code for iframe:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<base href="http://localhost/test/" target="_top" />
<noscript>
<meta http-equiv="refresh" content="1; URL=http://localhost/test/javascript_error.php" />
</noscript>
<meta http-equiv="refresh" content="10" />
<title>Detect Script</title>
</head>
<body style="text-align:center;">
    Detecting whether javascript script is enabled/disabled in the browser.
</body>
</html>

I'm adding the tag to the content of an iframe html and a redirect meta tag, which should load to the top parent location.

Any idea why this does not work? or how do i get this thing to work?

Thanks

Community
  • 1
  • 1
  • 2
    "I need to detect whether javascript is disabled in browser" — Are you sure? [Good design practises](http://icant.co.uk/articles/pragmatic-progressive-enhancement/) usually remove that need. – Quentin Aug 28 '12 at 08:44

2 Answers2

1

The base tag affects only elements with URLs that appear as the content of an attribute designated to have a URL value, such as href.

The constructive approach to dealing with JavaScript enabled/disabled is to first design a page assuming that JavaScript is disabled, then add JavaScript in a non-obtrusive way.

Jukka K. Korpela
  • 195,524
  • 37
  • 270
  • 390
0

UPDATE: I see what you are trying to do. The base tag will only work with links. Since this only is done when JavaScript is disabled, the href of the iframe cannot possibly change and you cannot possibly 'programmatically click' a link which may reference to that JavaScript error page, there is really no possible solution using base. Refer to my solution below.





The reason that your code doesn't work is because most browsers would not render 2 <meta http-equiv="refresh"> tags separately.

So instead of writing <meta http-equiv="refresh" content="10" />, just write:

<script type="text/javascript">
setTimeout(function () {
window.location.reload();
}, 10000);
</script>
Lucas
  • 16,930
  • 31
  • 110
  • 182
  • Thanks. But my concern is when somebody disabled script in between where javascript is not present and this solution fails there. So I am trying to make use of tag. Do you know any solution beyond javascript. Thanks in advance. –  Aug 28 '12 at 07:30
  • @Ashis I don't see what you're trying to do. What I think is that you will redirect the user to another page is javascript does not work. Otherwise, you will stay on the current page, and then reload it after 10 seconds. I don't see where I went wrong. – Lucas Aug 28 '12 at 07:47
  • @Ashis I slightly modified it, take a look – Lucas Aug 28 '12 at 09:13