开发者

Is it possible to load Jquery in the body of a page

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://f开发者_高级运维ormulator.codingproject.net/content/login/

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


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 :)


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>


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.


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.


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.


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜