Javascript in a Choice?

Javascript in a Choice?

by Rob Hindman -
Number of replies: 3
I've started using the Choice module on a Moodle site I'm developing, but have run into a problem.  It seems that when I insert Javascript into the "Choice text" of a Choice (that is, the intro or description), it does not get processed/rendered when the Choice is viewed in Moodle.

What I am doing is clicking the "<>" button in the WYSIWYG editor and then pasting in a few simple lines, like so:

<script language="javascript">
 progCheck1();
</script>

That function is, of course, defined in my Javascript code that is referenced in the HTML header.  I've had great success using Javascript in other modules, such as Resource and Assignment, but for some reason it's not working in Choice.  When the Choice is viewed, all one sees is the text:

progCheck1();

So basically it seems the <script> tags are being stripped or filtered out.  Anyone know anything about this or have any insight into what I need to modify to remedy this? (I'm experienced with PHP, but just thought I'd cast a line to see if anyone has encountered this before).
Average of ratings: -
In reply to Rob Hindman

Re: Javascript in a Choice?

by Dan Marsden -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers Picture of Plugins guardians Picture of Testers Picture of Translators
Hi Rob,

it's pretty much a security risk to allow people to input javascript into forms etc. - lots of moodle code use the function format_text which strips some of this stuff out.

The choice Module uses this function to strip out all "dangerous" content before displaying it. - All Modules 'should' do this for content that is entered by a user.

do a search for this function in mod/choice/view.php to see where/how it is called.

smile

Dan
In reply to Dan Marsden

Re: Javascript in a Choice?

by Rob Hindman -
Thanks, Dan.  The security issue is not a big deal in my scenario since I will be the only person who will be able to enter text on choices on this site.  In case anyone else comes along in a similar situation, here is what I've done to enable Javascript in Choice text:

In mod/choice/view.php, find this (line 115 or so):

if ($choice->text) {
        print_simple_box(format_text($choice->text, $choice->format), 'center', '70%', '', 5, 'generalbox', 'intro');
    }

And replace with this:

if ($choice->text) {
        $formatoptions->noclean = true;
        print_simple_box(format_text($choice->text, $choice->format, $formatoptions), 'center', '70%', '', 5, 'generalbox', 'intro');
    }
In reply to Dan Marsden

Re: Javascript in a Choice?

by Rob Hindman -
Forgot to mention that the above code changes apply to 1.5.4.  I have not looked at the code for the just-released 1.6.