Javascript Problem (1)

Javascript Problem (1)

napisao/la Arthur Hung -
Number of replies: 24

I'm trying to add a pop up link using the html editor, but it does something funny every time.
This is what i did,
(1) I click on "edit" for a lesson or journal
(2) I switch to "html source" in order to enter the predefined pop up link,

<A Xonclick="window.open(this.href,'','top=200,left=200,width=400,height=180');return false;" href="http://www.somesite.com/user_pages/senses.html"><U>senses</U></A>

(3)After i switch back to the "regular" mode and back to the "html source" mode the link changes to this,

<a Xonclick="function anonymous()
{
window.open(this.href,'','top=200,left=200,width=400,height=180');return false;
}" href="senseshttp://www.somesite.coml/user_pages/senses.html"><u>senses</u></a>

The editor keeps adding layers if i repeat (2) & (3),

<a Xonclick="function anonymous()
{
function anonymous()
{
window.open(this.href,'','top=200,left=200,width=400,height=180');return false;
}
}" href="senseshttp://www.somesite.com//user_pages/senses.html"><u>senses</u></a>

I tried using a regular anchor tag that with target="_blank", but i don't want the extra options (ie. scroll bars, menu bar, status bar, etc etc).

I'm using moodle 1.2.1. It didn't do that on moodle 1.1.1
Is there a way to work around this?
Is there a way to set the editor to accept javascripts?

Any help would be greatly appreciated!

Prosjek ocjena: -
In reply to Arthur Hung

Re: Javascript Problem (1)

napisao/la Arthur Hung -
I don't know why, but an "X" and the word "senses" (before http://) is being added to my previous post by the editor. Is that another problem?
In reply to Arthur Hung

Re: Javascript Problem (1)

napisao/la Martin Dougiamas -
Slika Core developers Slika Documentation writers Slika Moodle HQ Slika Particularly helpful Moodlers Slika Plugin developers Slika Testers
No, the X is a security feature to prevent Javascripts being inserted by students. I don't know about the other re-writing - I've posted your code below and it seems to work as expected.


senses
In reply to Martin Dougiamas

Re: Javascript Problem (1)

napisao/la Arthur Hung -

Have you tried toggling from WYSIWYG view to html source view?

I noticed that if i just paste the code and save, it works fine. But if i need to edit the content multiple times and switch back and forth from the 2 views, then the problem occurs!

In reply to Martin Dougiamas

Re: Javascript Problem (1)

napisao/la Carl Nagle -

I get this exact same problem.  The editor adds the non-functional "function anonymous" javascript code which must be manually removed EVERY SINGLE TIME you go to edit any text in the editor. I got this with moodle 1.3.somethingorother and I get it still after upgrading to 1.4.1+ on Sept 18.

But in 1.3.1 I could at least remove the bad anonymous text and the JavaScript would work.  Now, in 1.4.1 the JavaScript NEVER executes because 'new student security measures' is adding that 'x' in front of 'onClick' -- making it 'xonClick'.  It did not do this with 1.3.X  And I am the teacher/instructor/admin.

I haven't found where this is happening in the code.  I have no idea how I can fix it.  But it seems to leave me little choice but to delete my 1.4.1+ install and revert back to 1.3.X

Say it isn't so!

In reply to Martin Dougiamas

Re: Javascript Problem (1)

napisao/la Carl Nagle -
So how do we get rid of this X for teachers and admins?
In reply to Carl Nagle

Re: Javascript Problem (1)

napisao/la Martin Dougiamas -
Slika Core developers Slika Documentation writers Slika Moodle HQ Slika Particularly helpful Moodlers Slika Plugin developers Slika Testers
The problem with the editor rewriting scripts is an old one for which I don't know the cause or the answer.

The Moodle filtering process which strips scripts from places where students can enter text is something I know about though. In places like HTML pages, section summaries, labels, activity introduction texts etc the javascript is not stripped.

Where exactly are you wanting to include this Javascript as a teacher? In forum posts? Why?
In reply to Martin Dougiamas

Re: Javascript Problem (1)

napisao/la Carl Nagle -
My example is in the lesson 'question page', I believe it is called.  In some instances I have a graphic or figure that I only show a thumbnail for because it is just too large to statically display.  I want to use openurlpopup when the user clicks the thumbnail so they can reference the larger graphic in a separate window as they read the accompanying text.  And I want that to be a window with minimal other content (toolbars, etc...)
In reply to Carl Nagle

Re: Javascript Problem (1)

napisao/la Martin Dougiamas -
Slika Core developers Slika Documentation writers Slika Moodle HQ Slika Particularly helpful Moodlers Slika Plugin developers Slika Testers
OK, then this is not a bug with the filters themselves, but a bug in Lesson.

You just need to file a bug report in the bug tracker to get the format_text call fixed so that includes the option to not clean the Lesson text.
In reply to Arthur Hung

Re: Javascript Problem (1)

napisao/la Kevin Lawry -

I can't help with  a solution, but I can confirm that I get a very similar problem trying to insert VB script that I use on my intranet to launch local applications.  The only way I have found around it is to turn off the html editor & do it long hand.

I am still running an earlier version though, things may have changed in the latest release (i think I am on the last 1.2 releas

In reply to Kevin Lawry

Re: Javascript Problem (1)

napisao/la Carl Nagle -
This does not work for me.  The text INSIDE the editor -- even the simple form editor -- remains as 'onclick='.  It is when the page gets compiled and rendered that the 'onclick' is converted to 'xonclick' rendering the javascript useless.  (Notice even the plain text in this note had something convert my initial 'onclick' text above to 'xonclick'.  Seems to be associated with the accompanying = equality sign.)
In reply to Arthur Hung

Re: Javascript Problem (1)

napisao/la James Robertson -

Has this problem been solved?  I am trying to create a link that opens a smaller window to a reading chapter, with the resource still visible while the student scrolls through the chapter.  The resource is created in the HTML editor ("create a web page") using text mode.  When I insert the code

<a href="javascript:void(0)" onclick="javascript:window.open('http://www.google.com', '', 'height=550,width=580')">

I get the "function anonymous" error described in this thread (google is just for testing).  I am using Moodle 1.4.4+, 17Apr05.

Thanks,

Jim Robertson.

In reply to James Robertson

Re: Javascript Problem (1)

napisao/la Pablo Etcheverry -
Slika Core developers
This problem is not solved and it remains in Moodle 1.5.3+ (2005060230). It's very annoying, since I can not ask a regular teacher to use an external editor...
Does anyone have any clue??

TIA,
PAblo
In reply to Pablo Etcheverry

Re: Javascript Problem (1)

napisao/la Joseph Rézeau -
Slika Core developers Slika Particularly helpful Moodlers Slika Plugin developers Slika Testers Slika Translators
When I copy and paste the Javascript code given by James Robertson in message above inside a resource (create new Web page), in the HTML source, of course... it works for me.
Works in Moodle 1.5.3 stable and 1.6 beta 5, MSIE and Firefox.
Code pasted: <a href="javascript:void(0)" onclick="javascript:window.open('http://www.google.com', '', 'height=550,width=580')">Click here</a>
NOTE: onclick in my code is transformed to xonclick on this forum0
Joseph
Pablo, it might help if you said more precisely what kind of Javascript you want to embed and in which type of resource/block/module of Moodle.
In reply to Joseph Rézeau

Re: Javascript Problem (1)

napisao/la Pablo Etcheverry -
Slika Core developers
After some testing I've reached to the following description of the problem. I did it on a plain HTML resource:

  1. Paste this in HTML mode:
    <p class="titular">Autor: Jaun Pérez <a target="entry" class="glossary autolink glossaryid164" 
    href="http://mysite.com/mod/glossary/showentry.php?courseid=38id=2666&eid=3482"
    onclick="return openpopup('/mod/glossary/showentry.php?courseid=38&id=2666&eid=3482',
    'entry','menubar=0,location=0,scrollbars,resizable,width=600,height=450', 0);">
    <img src="http://mysite.com/file.php/38/lupita.gif" /> </a> </p>
  2. Save the resource
  3. Edit the resource (it opens in WYSIWYG mode by default)
  4. Switch to HTML mode. The following appears:
    <p class="titular">Autor: Jaun Pérez <a onclick="
    function onclick(event) {
    return openpopup("/mod/glossary/showentry.php?courseid=38&id=2666&eid=3482", "entry", "menubar=0,location=0,scrollbars,resizable,width=600,height=450", 0);
    }
    " href="http://mysite.com/mod/glossary/showentry.php?courseid=38id=2666&eid=3482" class="glossary autolink glossaryid164" target="entry">
    <img src="http://mysite.com/file.php/38/lupita.gif" /> </a> </p>
  5. First surprise. Go mad. Don't save anything and edit the resource again in WYSIWYG mode. Change "Jaun" for "Juan". Save.
  6. Edit the resource again. Try to switch to HTML mode. No response. Firefox tells me there is a mistake with javascript.
Hope this describes better the problem.
Cheers,
Pablo
In reply to Pablo Etcheverry

Re: Javascript Problem (1)

napisao/la Joseph Rézeau -
Slika Core developers Slika Particularly helpful Moodlers Slika Plugin developers Slika Testers Slika Translators
Hi Pablo,
Sorry, but I still don't understand what exactly it is that you want to do with this example. Why would you want to insert a piece of javascript code which links to a glossary word when such piece of javascript will be automatically inserted by Moodle?
Is this a real-world example? I'm puzzled.surprise
Joseph
In reply to Joseph Rézeau

Re: Javascript Problem (1)

napisao/la Pablo Etcheverry -
Slika Core developers
Don't be so puzzled, Joseph namiguje This is absolutely real.
This problem has been addressed several times in the forums. Many times a teacher wants to make a definition for a very common term, which has a lot of sense in the context where it is used, but no sense at all out of there.
Suppose she wants to use the term "our" (it's a real case, in spanish of course). It is associated with a Sociological term. In this case, if we enabled autolinking, the term would appear everywhere, even in places where that definition has nothing to do (even in Forums!). So, we HAVE to make a manual link, we haven't found another option.
Maybe you have another options... and I'd be very grateful if you shared them with me. Anyway, we need a fix for the editor, since we have hundreds of Book pages composed this way and we can't change them during this time of the year.
TIA,
Pablo
In reply to Pablo Etcheverry

Re: Javascript Problem (1)

napisao/la Joseph Rézeau -
Slika Core developers Slika Particularly helpful Moodlers Slika Plugin developers Slika Testers Slika Translators
OK Pablo,
Now I understand what you want to do and why. I will give it a try in a few days' time, as I'm away from my base at the moment (working in Paris for a couple of days).
Joseph
In reply to Joseph Rézeau

Re: Javascript Problem (1)

napisao/la Pablo Etcheverry -
Slika Core developers
Hi, Joseph!
Were you able to find something about this? With every edition another "function anonymous()" is added...

Thanks a LOT!
Pablo
In reply to Pablo Etcheverry

Re: Javascript Problem (1)

napisao/la Joseph Rézeau -
Slika Core developers Slika Particularly helpful Moodlers Slika Plugin developers Slika Testers Slika Translators
Hi Pablo,

I could not reproduce your problem. I never get the "function anonymous()" error. However, I have managed to make things work.

Specifications: Moodle 1.6 (beta 5); Windows XP; FireFox 1.5 and MSIE 6. Local install (Apache, etc.).

  1. Supposing the word you want to link manually, not autolinked is the word "problem"
  2. Go to one of your course's glossaries.
    1. New entry: problem. Check This entry should be automatically linked. Definition: a situation that causes difficulties.
    2. New entry: difficult. Check This entry should be automatically linked. Definition: not easy , which can cause a problem or problems.
  3. In one of your course topice, Edit mode, Add a Resource, Compose a Web Page. In normal (WYSIWYG) mode, type: "Testing Pablo's difficult little problem with Moodle here."
  4. Save. View your "Pablo" Resource. Words difficult and problem are displayed with links to the Glossary. Select the whole text "Testing Pablo's..." In your browser (FireFox), right-click and View Selectec Source Code. Copy and Paste this source code to NotePad (or a similar simple text editor, not a Word Processor!.
  5. Source code looks something like this:
    Testing Pablo's <a target="entry" class="glossary autolink glossaryid18" title="test glossary: difficult" href="http://.../mod/glossary/showentry.php?courseid=9&amp;concept=difficult" onclick="return openpopup('/mod/glossary/showentry.php?courseid=9\&concept=difficult', 'entry', 'menubar=0,location=0,scrollbars,resizable,width=600,height=450', 0);">difficult</a> little <a target="entry" class="glossary autolink glossaryid18" title="test glossary: problem" href="http://.../mod/glossary/showentry.php?courseid=9&amp;concept=problem" onclick="return openpopup('/mod/glossary/showentry.php?courseid=9\&concept=problem', 'entry', 'menubar=0,location=0,scrollbars,resizable,width=600,height=450', 0);">problem</a> with Moodle here.
  6. Copy the code concerning the word you want to link manually, not automatically, the word "problem" (in blue above). You can simplify it like this:
    <a target="entry" class="glossary autolink glossaryid18" title="test glossary: problem" href="#" onclick="return openpopup('/mod/glossary/showentry.php?courseid=9\&concept=problem', 'entry', 'menubar=0,location=0,scrollbars,resizable,width=600,height=450', 0);">problem</a>
  7. Go back to your Glossary. Edit entry: problem. Uncheck This entry should be automatically linked. and save.
  8. Come back to your Pablo Resource, refresh browser: the word problem is no longer autolinked.
  9. Edit the Pablo Resource. Go to the HTML code. Replace the word problem with the code copied (see §6 above). The code of the Pablo resource now looks like this:
    Testing Pablo's difficult little <a target="entry" class="glossary autolink glossaryid18" title="test glossary: problem" href="#" onclick="return openpopup('/mod/glossary/showentry.php?courseid=9\&concept=problem', 'entry', 'menubar=0,location=0,scrollbars,resizable,width=600,height=450', 0);">problem</a> with Moodle here.
  10. Save. View the Pablo Resource: the word "difficult" is autolinked, the word "problem" has been manually linked. Click on the word "problem": the Glossary window opens... but it is empty: no word, no definition in it.
  11. The problem is that because the word "problem" has been entered as "not automatically linked" in our Glossary, the \glossary\lib.php file refuses to show it in the Glossary popup window. This is going too far, because if a word can be linked, automatically or manually, then it should be displayed. Anyway, there is a simple workaround - if you are ready to hack your Moodle PHP.
  12. Open moodle\mod\glossary\lib.php. Around line 520 (in Moodle 1.6 beta 5, should not be too far in Moodle 1.5+) delete the line in red.
    WHERE m.name = 'glossary' AND
    etc.
    e.usedynalink != 0 AND
  13. Et voila!
I have tried and tested every step described here, but cannot guarantee it will work on a moodle install with different specifications from the ones I stated at the beginning of this post.

Joseph

Attachment image-0003.jpg
In reply to Joseph Rézeau

Re: Javascript Problem (1)

napisao/la Joseph Rézeau -
Slika Core developers Slika Particularly helpful Moodlers Slika Plugin developers Slika Testers Slika Translators
Hi Pablo,
Did that help?
Joseph
In reply to Joseph Rézeau

Re: Javascript Problem (1)

napisao/la Pablo Etcheverry -
Slika Core developers
Hi, Joseph!

Your answer has been very kind and complete. I thought I had already answered it, sorry. tímido
Your solution is even better for this use, we had previously tested a very similar one but since it required a code modification, I discarded it. Anyway, I have applied you proposed solution and it works perfectly well, THANKS A LOT!

With this said, the problem with Javascript is still there. This workaround is great for the FUTURE work but I have hundreds of pages of HTML and three thousands of glossary items already loaded. I cannot ask the editors staff to change all of those links...
Maybe I have to wait until we can integrate another editor.
Thanks again,

Pablo

In reply to Joseph Rézeau

Re: Javascript Problem (1)

napisao/la Tony Parke -

Hi Joseph,

Would you be so kind as to help me with a javascript problem? I would like to add a drop down box, within a html block, that contains several lines of links to external websites, so the student makes a selection from the drop down box and is then automatically directed to the linked website. I am not sure how Moodle handles javascript, I have tried putting the code in the html block itself but the links just don't work. I would be grateful for any guidance.

Thanks,
Tony

In reply to Arthur Hung

Re: Javascript Problem (1)

napisao/la Pablo Etcheverry -
Slika Core developers
Hi!

Looks like this problem has not yet been solved. I found it in several places: Lesson, Book, Label... Martin D. dixit:

The problem with the editor rewriting scripts is an old one for which I don't know the cause or the answer.

This is an excerpt from a 2004 posting... is it still valid? Do we have to wait until we can use another editor?
Cheers,
Pablo

P.S. Joseph Rézeau, thanks again for the workaround you gave me before, it worked like a charm.