Jquery only working in the principal page?

Jquery only working in the principal page?

by Carlos Zurera Andrés -
Number of replies: 6

Hi!!

I have the following problem:

I have a custom block with two jquery files and jquery is only working in the principal page, when I go inside into a course jquery doesn't work.

I put the following lines in version.php:

$PAGE->requires->js('/lib/jquery/jquery-1.7.2.min.js', true);
$PAGE->requires->js('/lib/jquery/main.js', true);

I tried putting this lines in the principal block file:

$PAGE->requires->js('/lib/jquery/jquery-1.7.2.min.js');

$PAGE->requires->js('/lib/jquery/main.js');

Or:

$PAGE->requires->js('/lib/jquery/jquery-1.7.2.min.js', true);

$PAGE->requires->js('/lib/jquery/main.js', true);

But with these lines moodle doens't load...

Where is the error? How can I fix it?

Thanks a lot!!


Average of ratings: -
In reply to Carlos Zurera Andrés

Re: Jquery only working in the principal page?

by Darko Miletić -
In reply to Darko Miletić

Re: Jquery only working in the principal page?

by Carlos Zurera Andrés -

Thanks again for your answer Darko but I don't understand very well that post.

I think I don't use YUI, Have I to use it in order to work with these jquery files?


Im so sorry for my ignorance :/

In reply to Carlos Zurera Andrés

Re: Jquery only working in the principal page?

by Darko Miletić -

You can not include jquery like you did. In order to use jquery in Moodle you have to use the library shipped with Moodle. Hence:

$PAGE->requires->jquery();

That includes jquery withing Moodle page.

Rest of the information on that topic (how to use jQuery in Moodle) is listed on this wiki page.

http://docs.moodle.org/dev/jQuery

Also, you need to include jquery on every page you plan on using it.



Average of ratings: Useful (1)
In reply to Darko Miletić

Re: Jquery only working in the principal page?

by Carlos Zurera Andrés -

I tried the following lines in the principal php file of my block:

//$PAGE->requires->jquery();
 //$PAGE->requires->js('/lib/jquery/jquery-1.7.2.min.js');
 //$PAGE->requires->js('/lib/jquery/main.js');

OR:

//$PAGE->requires->jquery();

 //$PAGE->requires->js('/lib/jquery/jquery-1.7.2.min.js', true);
 //$PAGE->requires->js('/lib/jquery/main.js', true);

OR:

//$PAGE->requires->jquery();

 //$PAGE->requires->js('blocks/access_mouse/jquery/jquery-1.7.2.min.js', true);
 //$PAGE->requires->js('blocks/access_mouse/jquery/main.js', true);

but every option returns me the same: Page not found.

What do u mean with "you need to include jquery on every page you plan on using it."?

Now is working fine in the principal page of moodle including only these lines in version.php of my block. I want to use jquery in the full page of moodle...


In reply to Carlos Zurera Andrés

Re: Jquery only working in the principal page?

by Darko Miletić -

I see that you do not understand.

Did you at least read the wiki page I provided? You have to read the documentation and try to understand.

Whenever you need to use Jquery in html page you need to include jquery.js. In Moodle

$PAGE->requires->jquery();

is equivalent of that. It is as if you did this in raw HTML:

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>Demo</title>
</head>
<body>
    <script src="/path/to/jquery.js"></script>
    <script>
    // Your code goes here.
    </script>
</body>
</html>

So basic moodle page would look like this:

require_once('/path/to/config.php');

$PAGE->set_url('/path/to/nameofscript.php');
$PAGE->set_course($SITE);
$PAGE->set_title('Sample');
$PAGE->set_heading('Sample');
// Include Jquery
$PAGE->requires->jquery();

// Add some jquery plugins for example jQuery-ui
$PAGE->requires->jquery_plugin('ui');
$PAGE->requires->jquery_plugin('ui-css');

echo $OUTPUT->header();

// Print something here

echo $OUTPUT->footer();





Average of ratings: Useful (2)
In reply to Darko Miletić

Re: Jquery only working in the principal page?

by Carlos Zurera Andrés -

Im so sorry Darko,


Of course I read before that wiki page but I couldn't understand. Finally I solved it adding my jquery files in the different HTML folders. U were a really good help.


Thanks again smile