views:

89

answers:

6

Hi everyone,

I am using code-igniter, and some of my views require jquery. Because they must be used in multiple places they must call jquery in their file, however since they are referencing an external file, calls to $(document.ready) are evaluated before loading jquery and therefore fail. Is it possible to put jquery in the body and still have it load before an javascript is evaluated. Or alternatively, is the some way to pass the fact that jquery is required back through code-igniter into the headers, which were callled before the file in question.

In a view:

echo $this->import->js('jquery.js','jquery');
echo '<script type="text/javascript">
$(document).ready(function(){$(\'div#login.rounded\').corner();})
</script>';

You can view the page at: http://formulator.codingproject.net/content/login/

NOTE This page actually resides on my home machine, so it is expected that the recaptcha fails.

+1  A: 

It looks like you are using PHP? If so, create a static method that returns that string, but only if it hasn't already been included this request. Then you can ensure that it's only being included once.

Zack
I'm already doing that. But it doesn't fix the problem.
lemiant
+1  A: 

I guess the answer is yes. you can load the jQuery.js in your body. But you have to write your script tags only after jQuery.js declaration, if not you may end up with errors :)

PS : please correct me If I'm wrong :)

Ninja Dude
That's what I'm doing right now, and it doesn't seem to work.
lemiant
+2  A: 

jQuery should really be called in the head element. Here's how you'd do that conditionally (untested).

In your controller, each function that needs jQuery should have:

$data['need_jquery'] = true;
$this->load->view('header');

In your header view:

<head>
    <? if($need_jquery) { ?>
        <script src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.2.min.js" /></script>
    <? } ?>
</head>
Ash White
Wow, that's a much better idea than mine.
Rocket
A: 

My website is the same way. What I do is I have one header that is loaded on all pages. In that header I do if($this->uri->segment(2) == 'controller'). Then I load jQuery and certain scripts if needed for that controller.

Rocket
A: 

I think it will be fine if jQuery will be included in the tag of every page, besides, you can use the minified version of jQuery which is not so heavy.

dmitko
A: 

Uh, maybe I'm wrong. But when I view your source code and follow where the jquery file is: http://www.formulator.com/assets/scripts/jquery/jquery.js I get a "page cannot be found" error. So I'm guessing this could be the problem. Maybe the way your php outputs it isn't including the correct domain/subdomain?

Matthew
Oh, sorry. Like I said it resides on my home machine, so I'll need to update it a bit to access jquery.
lemiant
Alright I've fixed that. But the error persits.PS thanks for the tip off.
lemiant