By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,089 Members | 1,502 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,089 IT Pros & Developers. It's quick & easy.

Detecting if JavaScript is enabled

P: n/a
Hi,

This is certainly a simple question, but I'm a newbie in JavaScript.

Is there a way to know if JavaScript is enabled?

My php script uses JavasSript to do something. If JavaScript is not
enabled, I can put a "submit" push button. But I don't want both (the
"submit" push button with the automatic action), so I want to know on
the fly which one I must use (whether JavaScript is enabled).
Aug 20 '08 #1
Share this Question
Share on Google+
12 Replies


P: n/a
Anic297 wrote on 20 aug 2008 in comp.lang.javascript:
Hi,

This is certainly a simple question, but I'm a newbie in JavaScript.

Is there a way to know if JavaScript is enabled?
<script type='text/javascript'>
document.write('Javascript is enabled');
</script>
My php script uses JavasSript to do something.
Impossible, as PHP only runs serverside, and Javascript runs clientside.

[Well it can run serverside on ASP, but not on PHP]
If JavaScript is not
enabled, I can put a "submit" push button. But I don't want both (the
"submit" push button with the automatic action),

<input type='submit' id='myButton'>
<script type='text/javascript'>
document.getelementById('myButton').style.display = 'none';
</script>
so I want to know on
the fly which one I must use (whether JavaScript is enabled).
That is not about you knowing, but about programming for it.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Aug 20 '08 #2

P: n/a
Anic297 wrote:
Is there a way to know if JavaScript is enabled?
I can write you a script if you can write me a program that detects if the
computer is running that the program is running on.
My php script uses JavasSript to do something.
That's unlikely.
If JavaScript is not enabled, I can put a "submit" push button. But I
don't want both (the "submit" push button with the automatic action), so
I want to know on the fly which one I must use (whether JavaScript is
enabled).
<form action="foo.php" onsubmit="...; return false">
...
<input type="submit" ...>
...
</form>

This was a FAQ, please search the archives before you post.
<http://jibbering.com/faq/>
PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
Aug 20 '08 #3

P: n/a
Evertjan. a écrit:
>My php script uses JavasSript to do something.

Impossible, as PHP only runs serverside, and Javascript runs clientside.
Yes... I have to learn that...
>If JavaScript is not
enabled, I can put a "submit" push button. But I don't want both (the
"submit" push button with the automatic action),

<input type='submit' id='myButton'>
<script type='text/javascript'>
document.getelementById('myButton').style.display = 'none';
</script>
Oh, yes, hiding the button from JavaScript only if it's enabled!
>so I want to know on
the fly which one I must use (whether JavaScript is enabled).

That is not about you knowing, but about programming for it.
Well, I do desktop programming, but web programming is not the same thing!

Thank you!
Aug 20 '08 #4

P: n/a
Thomas 'PointedEars' Lahn a écrit:
>Is there a way to know if JavaScript is enabled?

I can write you a script if you can write me a program that detects if the
computer is running that the program is running on.
Well... perhaps not a JavaScript script...
(but I have had my answer)
>My php script uses JavasSript to do something.

That's unlikely.
Well, I don't use correct terms (I'm new in web programming). I just
expected readers to understand my phrases.
This was a FAQ, please search the archives before you post.
<http://jibbering.com/faq/>
Thanks
Aug 20 '08 #5

P: n/a
Anic297 wrote:
Hi,

This is certainly a simple question, but I'm a newbie in JavaScript.

Is there a way to know if JavaScript is enabled?
Ignoring the details of what you'd do on the server side with this
knowledge, and even whether knowing this is generally useful, and your
application can work with an initial form that doesn't need to
specifically know whether JavaScript is available or not, you could
then have a script that runs on the page that uses the DOM api to add
a hidden input element to the form, where the hidden value indicates
that JavaScript is present. When the form is submitted, either by the
user or by JavaScript, the server-side code would then be informed
whether the browser had JavaScript enabled.
Aug 20 '08 #6

P: n/a
Anic297 wrote:
Evertjan. a écrit:
>>My php script uses JavasSript to do something.
Impossible, as PHP only runs serverside, and Javascript runs clientside.

Yes... I have to learn that...
And to unlearn it afterwards. Neither language is restricted to client-side
oder server-side use. It is only that PHP is more often used server-side,
while ECMAScript implementations are probably more often used client-side.
>>If JavaScript is not
enabled, I can put a "submit" push button. But I don't want both (the
"submit" push button with the automatic action),
<input type='submit' id='myButton'>
<script type='text/javascript'>
document.getelementById('myButton').style.display = 'none';
document.getElementById(...)...

But document.forms[...].elements["myButton"] is more compatible.
></script>

Oh, yes, hiding the button from JavaScript only if it's enabled!
This solution can be recommended against as it introduces a dependency on
the CSS DOM needlessly.
PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
Aug 20 '08 #7

P: n/a
Evertjan. a écrit:
<input type='submit' id='myButton'>
<script type='text/javascript'>
document.getelementById('myButton').style.display = 'none';
</script>
Hi,
Despite the fact that the code is simple, I can't get it to work.
Here's the source code from my browser:
Select a QuickTime Movie:
<FORM ACTION="./moviesindex.php?movieskind=qt" METHOD="post" NAME="nav2">
<SELECT NAME="url"
onChange="document.location.href=document.nav2.url .options[document.nav2.url.selectedIndex].value">
<OPTION VALUE=./moviesindex.php?movieskind=qt&moviename=mov1>Mouth
<OPTION VALUE=./moviesindex.php?movieskind=qt&moviename=mov2>Eyes
.... (other options)
</SELECT>

<INPUT TYPE="SUBMIT" id="MovieButton" VALUE="Clic here if the page does
not update">

<script type='text/javascript'>
document.getelementById("MovieButton").style.displ ay = "none";
</script>
</FORM>

I think it should not make any difference (since the code above is the
result), but all is done in php (I send all using the "echo" command).

I also tried using "<script language='Javascript'>".

It's hard when you know programming but not web programming: you know
logical ways of doing things, but the syntax and principles are totally
differents (I start from scratch)!

What do you suggest?
Aug 20 '08 #8

P: n/a
Thomas 'PointedEars' Lahn a écrit:
And to unlearn it afterwards. Neither language is restricted to client-side
oder server-side use. It is only that PHP is more often used server-side,
while ECMAScript implementations are probably more often used client-side.
Ok...
>> document.getelementById('myButton').style.display = 'none';

document.getElementById(...)...
You know, I had replied to "Evertjan."'s post because it didn't worked
(even with differences). Now, I have removed my unnecessary post ;-)
But document.forms[...].elements["myButton"] is more compatible.
Ok...
>Oh, yes, hiding the button from JavaScript only if it's enabled!

This solution can be recommended against as it introduces a dependency on
the CSS DOM needlessly.
I think it's an interessant approach, anyway.

Thank you for all this!
Aug 20 '08 #9

P: n/a
If JS is NOT enabled there is nothing you can do in the client side so you
must write your code assuming it is off.

Have this <body onload='js_exist_code' Then if JS is active (on) the
function 'js_exist_code' runs and changes stuff to the JS version. A lot
can be done by just using style stuff and changing className of some
elements.

This fails if the user turn JS after loading. <g>

HTH

Aug 20 '08 #10

P: n/a
david.karr a écrit:
You could then have a script that runs on the page that uses the DOM api
to add a hidden input element to the form, where the hidden value
indicates that JavaScript is present. When the form is submitted,
either by the
user or by JavaScript, the server-side code would then be informed
whether the browser had JavaScript enabled.
Yes, that would work if I want to know the enabled state of JavaScript
when data are submitted. In my case, I want to know that when the page
is being written.

Thanks, it will be good to know for the future.
Aug 20 '08 #11

P: n/a
Dr***********@nyc.rr.com a écrit:
If JS is NOT enabled there is nothing you can do in the client side so you
must write your code assuming it is off.
Well, I have an alternative (either it's called by an event in
JavaScript or a push button appears). In this case, I think it's good to
know.
Have this <body onload='js_exist_code' Then if JS is active (on) the
function 'js_exist_code' runs and changes stuff to the JS version. A lot
can be done by just using style stuff and changing className of some
elements.
Hmmm... I'm not yet at this degree of knowledge ;-)
This fails if the user turn JS after loading. <g>
Good point, indeed.
(in my actual case, it's worse if the user turns JS on, not off, after
loading.
Thank you.
Aug 20 '08 #12

P: n/a
Evertjan. a écrit:
<input type='submit' id='myButton'>
<script type='text/javascript'>
document.getelementById('myButton').style.display = 'none';
</script>
Yes, it works (as expected)! Thank you very much!
Aug 20 '08 #13

This discussion thread is closed

Replies have been disabled for this discussion.