[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 233
  • Last Modified:

Javascript enable or disable?

Hi E´s, I get from another solution in EE the code you can see in snippet code. That code show a simple code to detect if js is enable or disable in the browser.
I need something similar of that code, but with some changes.
In practice I need a js script that detect if js is disable or enable in browser, and if is disable stop the rest of the code, and if is enable don't do nothing.
Off course, if is disable don't have any way to run the js script, but can have a solution with js and PHP that can do that.

Have a solution for this?

The best regards, JC

<html>
<head>
</head>
<body>
Hello
<script language="Javascript">
document.write("Good you have a javascript capable browser");
</script>
<noscript>
Sorry you need a javascript capable browser
</noscript>
</body>

Open in new window

0
Pedro Chagas
Asked:
Pedro Chagas
  • 9
  • 7
  • 2
  • +2
3 Solutions
 
strickddCommented:
<html>
<head>
</head>
<body>

<h1>My Site</h1>
<div>
   This is the content of my site.
</div>


<noscript>
Sorry you need a javascript capable browser
</noscript>
</body>
0
 
Pedro ChagasWebmasterAuthor Commented:
Hi, but how I stop the page, for customer don't see the contain?
If it is not possible, I have a idea with js + php, but please see if have a easy way to do that.
0
 
strickddCommented:
<html>
<head>
</head>
<body>
Hello
<script language="Javascript">
document.getElementById("MyContent").style.display = "";
</script>
<noscript>
Sorry you need a javascript capable browser
</noscript>
<div id="MyContent" style="display:none;">
      This is my content.
</div>
</body>

Open in new window


This will not show the content, but it will still be in the DOM and can be viewed when viewing the source. If that is a concern, then you can't achieve what you want via this method.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Pedro ChagasWebmasterAuthor Commented:
The idea is users can't have acess to the code, visible or not. I need something with more security, so for that I have a idea that join is and php:
The idea is js detect if enable or not, if it is enable js reload the page (for our example index.php) index.php whit a get variable, like index.php?js=on. If disable don't do nothing and the page reload normal.
In php I check if the var get "js" is on, and if is "on" the page reload normal, and if it is different of "on" php exit the code and the page stop running.

In php I don't have problem but in js I have, so I need to know how I detect js, and if is on reload for the index.php?js=on, is just that.

Can you do just the part of js?
0
 
Hugh McCurdyCommented:
How about if you have two pages.  The first determines

if Javascript
    Load page with content
else
    Show error
<script type="text/javascript">
<!--
window.location = "http://www.mydomain.com/mypagewithcontent.html"
//-->
</script>
<noscript>
    Sorry you need a javascript capable browser
</noscript>

Open in new window


0
 
Hugh McCurdyCommented:
You could also output the entire page with document.write() but that seems rather difficult.
0
 
experts1Commented:
Try use the page redirect strategy in <noscript> code as below:

<noscript>
  <META HTTP-EQUIV="Refresh" CONTENT="0;URL= nonscript_page.html">
</noscript>
0
 
Pedro ChagasWebmasterAuthor Commented:
@hmccurdy, I don't understand your question?!
0
 
Hugh McCurdyCommented:
experts1 has a pretty good idea too.

My idea was to have one page that determines if the user should be allowed to proceed to the Javascript enabled pages.  If JS is enabled, the desired page will be loaded.  If not, they'll get the message.

Does this make sense yet?
0
 
Pedro ChagasWebmasterAuthor Commented:
Yes, experts1 is a great idea, I will test, and in some minutes I will be back to close this question.
0
 
Pedro ChagasWebmasterAuthor Commented:
@hmccurdy, I want close this question, and I decide the best answer was from expert1, but before I close I'd like to see your last solution work, I tested I in all scenario (enable/disable) the page appear all in blank.
Can you post a work solution based in you last solution?
 
<html>
<head>
</head>
<body>
Hello
<script language="Javascript">
document.getElementById("MyContent").style.display = "";
</script>
<noscript>
Sorry you need a javascript capable browser
</noscript>
<div id="MyContent" style="display:none;">
      This is my content.
</div>
</body>

Open in new window

0
 
Pedro ChagasWebmasterAuthor Commented:
In the last post I meant @strickdd and not @hmccurdy. My apologizes!
0
 
Hugh McCurdyCommented:
I like expert1's answer too.  I think it's more obvious than mine related to self documenting what is going on.  Code that self documents is very desirable.
0
 
Ray PaseurCommented:
Just a note for your planning purposes.  By the time the JavaScript runs, the PHP that generated the HTML for the web page has completely finished its work.  So you can redirect the browser or present something with the <noscript> tag, but whatever HTML you generated and sent has gone to the client machine.

As a practical matter, I think you can assume that everyone has JavaScript.  You can't swing a dead cat without hitting an AJAX web site!  Google, Facebook, Yahoo, Twitter, ebay, etc... All of them require JS.  So any browser without JS support, or with JS turned off, is w-a-a-ay out in the edge cases.
0
 
Hugh McCurdyCommented:
I agree with Ray.  Pretty much the only time I have JS off is to test <noscript> sections.
0
 
Pedro ChagasWebmasterAuthor Commented:
Hi @hmccurdy, you try your code?
Hi Ray, good point of view!

Regards, JC
0
 
Hugh McCurdyCommented:
Sorry, I misread the request.  I created two files, foo.html and bar.html

foo.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
   "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
  <meta name="Author" content="Hugh McCurdy" />
  <script type="text/javascript">
    <!--
    window.location = "bar.html"
    //-->
  </script>
</head>
<body>
  <noscript>
    You need Javascript
  </noscript>
</body>
</html>

Open in new window


bar.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
   "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
  <meta name="Author" content="Hugh McCurdy" />
</head>
<body>
  This is bar.html
</body>
</html>

Open in new window


With JS off, I get    You need Javascript
With JS on, I get    This is bar.html
0
 
Hugh McCurdyCommented:
To clarify, that's the output I get when I load foo.html  
0
 
experts1Commented:
hmccurdy's code will do the trick also, but
I just wanted to demonstrate the possibility
to render HTML coding within the <noscript>
</noscript> tags without using javascript code.

Regard

0
 
Hugh McCurdyCommented:
There is more than one way to solve this sort of problem.  
0
 
Hugh McCurdyCommented:
Thanks for the points.  Just exceeded 150K points.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 9
  • 7
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now