Typically, when a webpage loads and Javascript is disabled in the browser, we use the <noscript>
tag to display a warning prompting the user to enable Javascript. Contrary to this standard practice, Facebook notifies users of disabled Javascript even after loading the page with JS enabled. How can I implement a similar feature?
UPDATE: Although this functionality is no longer available on Facebook, it was previously. My timing may have been off in asking this question, but any insights would be greatly appreciated.
My Attempts
I considered including a segment within my page that continuously checks if Javascript is disabled, and if so, displays the content from <noscript>
.
To achieve this, I created a page called CheckJS.html.
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="refresh" content="0">
</head>
<body>
<noscript>
JS is disabled!
</noscript>
</body>
</html>
This page will keep refreshing, displaying "JS is disabled!" when Javascript is turned off.
To incorporate this page into my original page, I tried the following:
1- .load()
I utilized JQuery to .load('CheckJS.html')
inside a div
. However, it appears that .load()
only fetches the contents of the <body>
of CheckJS.html, excluding the <head>
element and its content.
2- iframe
After some investigation, I discovered that the only way to load an entire HTML page, including the <head>
, is by using an <iframe>
.
<iframe src="CheckJS.html"></iframe>
Nevertheless, the
<meta http-equiv="refresh" content="0">
line in CheckJS.html affects the parent page, causing it to refresh as well.
If it's possible to employ this <iframe>
without triggering a refresh on the original page, then this could be a viable solution. However, it still feels more like a workaround rather than a definitive fix.
UPDATE
Antony's response demonstrated that my assumption about the iframe
refreshing the original page was incorrect. The browser indicates a refresh, but in actuality, it does not occur. If this proves true, then Javascript detection can be achieved without the need for Javascript. The provided CheckJS.html
accomplishes the task, and notably, the <noscript>
gets hidden once JS is re-enabled. Despite this insight, relying on the iframe method may not provide the best user experience (potentially freezing the browser), unless automatic refreshing occurs every 10 seconds or so, which doesn't constitute truly instant detection.