最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Is it possible to load Jquery in the body of a page - Stack Overflow

programmeradmin2浏览0评论

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

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

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/content/login/

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

Share Improve this question edited Jan 3, 2012 at 20:49 Josh Darnell 11.4k9 gold badges39 silver badges66 bronze badges asked Aug 31, 2010 at 19:36 lemiantlemiant 4,3654 gold badges32 silver badges39 bronze badges 4
  • I have attempted it and not found a way. I'll be interested to see if someone else has. – Joel Etherton Commented Aug 31, 2010 at 19:38
  • I'm not familiar with codigniter, but this seems interesting... what's the difference between $this->import->js('jquery.js','jquery') and just echo-ing the entire <script> manually? – Richard JP Le Guen Commented Aug 31, 2010 at 19:42
  • Sorry, import is a special object I built so that I wouldn't write reference to the same file multiple times. In short, it checks to make sure the file hasn't already been used and then returns the script, but it shouldn't affect anything. – lemiant Commented Aug 31, 2010 at 22:28
  • I've added a link... can anyone see why its not working? – lemiant Commented Aug 31, 2010 at 23:26
Add a ment  | 

6 Answers 6

Reset to default 2

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./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./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)

  1. 暂无评论