is javascript enabled?

is javascript enabled?

by Daniele Cordella -
Number of replies: 16
Picture of Core developers Picture of Plugin developers
I am developing an improvement I posted here.
I would like to know which Moodle function reports the remote user javascript activation in order to provide a different graphic user interface according to it.
In other words, which is the routine giving Moodle the way to decide to show or not to show (depending on the js activation) the "Go" button on the right of the "lang" menu in the header of the front page?

Thank you in advance.


Average of ratings: -
In reply to Daniele Cordella

Re: is javascript enabled?

by sam marshall -
Picture of Core developers Picture of Peer reviewers Picture of Plugin developers
This can't be done in PHP as it doesn't know. You need to write Javascript code to detect Javascript.

One way to handle this is to write both pieces of html to the page but mark them up as javascript and non-javascript, something like:

<div id="stuffwithjavascript" style="display:none">
...stuff with javascript...
</div>
<div id="stuffwithoutjavascript">
...stuff without javascript...
</div>
<script type="text/javascript">
document.getElementById('stuffwithoutjavascript').style.display='none';
document.getElementById('stuffwithjavascript').style.display='block';
</script>

(By the way the type of dropdown that activates when you select an option, rather than when you click a button, is bad practice for accessibility reasons; making it work without Javascript doesn't really address that. However obviously there are other uses for this technique such as maybe what you want to do.)

(The code above is sort of baddish practice too, because if you use a browser which doesn't have CSS support, it will show both chunks. Oh well.)

--sam


In reply to sam marshall

Re: is javascript enabled?

by Daniele Cordella -
Picture of Core developers Picture of Plugin developers
Thank you Sam for your detailed explanation.

Do you know how practically Moodle does to decide to show or not the button in the home page?
In reply to Daniele Cordella

Re: is javascript enabled?

by Álex González -
I'm very new on Moodle development, but Moodle doesn't uses a template system as smarty or templatePower??

Because If u use CSS to show/unshow the information will have 2 problems (I think):
-computional requeryments (only a little): because Moodle will "paint" 2 times the information, one showned and one unshowned
-accesibility problem: without CSS enabled the information is will see 2 times.

If u use template (i remember only templatePower) you can use:
<!-- START BLOCK : stuffwithjavascript -->
<!-- END BLOCK : stuffwithjavascript -->
<!-- START BLOCK : stuffwithoutjavascript -->
<!-- END BLOCK : stuffwithoutjavascript -->

And on the code if javascript is enables $tpl->newBlock("stuffwith/withoutjavascript");

But I don't know how Moodle treats the template system, but I think that it is the best way to do. But I remember u that I'm very newer on Moodle dev, haven't know yet as it works.

Regards!
In reply to Álex González

Re: is javascript enabled?

by sam marshall -
Picture of Core developers Picture of Peer reviewers Picture of Plugin developers
I agree with somebody else's assertion that templates in PHP are a crazy idea. PHP is already a template language. smile

Those templates will end up generating code similar to the code I pasted above, so there aren't likely to be significant performance or accessibility differences.

By the way you can avoid the issue about no-CSS support by using javascript to write the javascript block if necessary (instead of including it in the html but set to hidden in CSS). This is more hassle and does have possible issues with xhtml support (unless you go through using document.createElement for everything, which is feasible for short sections) but is probably better if you can be bothered.

As to the other question about how moodle actually does the go button specifically, I dunno, look in the code, it's probably in weblib.php.

--sam
In reply to sam marshall

Re: is javascript enabled?

by Álex González -
But with templates you can make the diference between code and style, with PHP is very difficult do this well.

I think that is a good idea use 2 diferrent php files (one with javascript and another without it), something as this:

<script type="text/javascript">
document.location.replace("path to the php with javascript");
</script>
THE HTML WITHOUT JAVASCRIPT

But... can be this slowy? because the server need to serve 2 webs if the javascript is enabled.

Bye!
In reply to Álex González

Re: is javascript enabled?

by Samuli Karevaara -
The template-thingy is a holy war, but still wanted to say: the separation of the code/content and style should be done with CSS, not with HTML trickery smile
In reply to Samuli Karevaara

Re: is javascript enabled?

by Álex González -
But when I develop a web page use this:

PHP (Script code) -> templatePower (HTML) -> CSS (Style for HTML)

Content -> Contener [o contenedor (my english isn't too good enough)] -> Design

Is easy and pretty.

But I don't know how Moodle do it, and the reason for do it. 1000's of programmers working on it can't be mistaked, I can ;)
In reply to Álex González

Re: is javascript enabled?

by Daniele Cordella -
Picture of Core developers Picture of Plugin developers
Thank you all but I am too simple to follow your discussion.

> As to the other question about how moodle actually does the go button specifically, I dunno, look in the code, it's probably in weblib.php.
This is my question!!!
I can't find it. Have you please seen it?
Can you, please, explain me where it is?
In reply to Daniele Cordella

Re: is javascript enabled?

by Samuli Karevaara -
My understanding is that the place in question doesn't detect javascript at all. It just hides the Go button with javascript immediately after it's been rendered to the client.

The code that produces this is in weblib.php, function popup_form(), there that part:
$output .= '<script type="text/javascript">'.
"\n//<![CDATA[\n".
'document.getElementById("noscript'.$formid.'").style.display = "none";'.
"\n//]]>\n".'</script>';

near the end.
In reply to Samuli Karevaara

Re: is javascript enabled?

by Daniele Cordella -
Picture of Core developers Picture of Plugin developers
thinking Eh?!
In reply to Daniele Cordella

Re: is javascript enabled?

by Paul Holden -
Picture of Core developers Picture of Moodle HQ Picture of Moodle Workplace team Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers Picture of Testers
Hi Danielle,

Look in index.php (in the root of your Moodle install), around line 94 you'll see

$langmenu = popup_form(...);

Which is then used in the print_header call a couple lines later. popup_form() is defined in lib/weblib.php - towards the end of this function you'll see that it uses javascript to hide the 'Go' button, meaning if javascript is enabled then the button will be hidden.

I wouldn't worry too much about it though wink
In reply to Paul Holden

Re: is javascript enabled?

by Daniele Cordella -
Picture of Core developers Picture of Plugin developers
Fantastic!!!! Thank you Samuli and Paul and all the group. Following your suggestions I got the solution I was looking for. Thank you, thank you and thank you again. I hope it can become with the time one more useful tool of Moodle.
In reply to Daniele Cordella

Re: is javascript enabled?

by Joseph Rézeau -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers Picture of Translators

Hi Daniele,

I should not worry too much about checking Javascript activation in the user's browser. In the Questionnaire module, the reordering questions page needs Javascript to be enabled to work anyway. Which means that if it is disabled, the user won't be able to re-order questions.

I have already expressed the view elsewhere in these forums that we should always make the assumption that Javascript is enabled on the end-user's machine. Wanting to surf the Internet these days with Javascript disabled (on the pretence of safety) is to me as ridiculous as wanting to drive a car with no petrol in the tank because petrol is a dangerous substance. If you are that paranoid, then stay at home/switch off your computer.evil

Joseph

In reply to Joseph Rézeau

Re: is javascript enabled?

by Daniele Cordella -
Picture of Core developers Picture of Plugin developers
You are right Joseph
I agree with you about js that has to be always enabled
but during development you have to take care also about paranoid people otherwise you are kicking them out and this is not nice at all. wink