jQuery在CodeIgniter中,在视图内还是在外部js文件? [英] jQuery in CodeIgniter, inside the view or in external js file?

查看:103
本文介绍了jQuery在CodeIgniter中,在视图内还是在外部js文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用CodeIgniter开发Web应用程序。所有这一次,我把自定义js代码在视图文件中做奇特的东西。通过这样做,我可以使用CodeIgniter提供的 site_url() base_url()函数。

I'm developing web application using CodeIgniter. All this time, I put the custom js code to do fancy stuffs inside the view file. By doing this, I can use site_url() and base_url() function provided by CodeIgniter.

今天我想将所有自定义js代码从视图文件分离成外部js文件。然后它打我,我不能在外部js文件中使用 site_url() base_url()。所以,我不得不把js代码移回视图文件。

Today I want to separate all custom js code from view file into an external js file. Then it hit me, I cannot use site_url() and base_url() in the external js file. So, I had to move the js code back into view file.

我想问这样的问题的意见,例子和最佳实践。你把自定义js代码放在视图中,还是放在外部js文件中?如果你把它放在外部文件中,你如何解决 site_url() base_url()除了当然放置我想避免的绝对url)。

I want to ask opinion, example, and best practice for this kind of problems. Do you put the custom js code inside the view, or in external js file? If you put it in external file, how do you get around the needs for site_url() and base_url() (beside of course put the absolute url that I want to avoid).

推荐答案

我通常把我的一个外部文件,我的模板(视图)中的一行,声明一个名为baseurl的javascript变量,以后可以由我的外部javascript使用。

I typically keep mine in an external file, but place a single line within my template (view) that declares a javascript variable called "baseurl" that can later be used by my external javascript.

<script type="text/javascript">
  var baseurl = "<?php print base_url(); ?>";
</script>
<script type="text/javascript" src="/js/scripts.js"></script>



现在我的scripts.js文件可以访问 base_url code>变量。

Now my scripts.js file has access to the base_url() value via its own baseurl variable.

这篇关于jQuery在CodeIgniter中,在视图内还是在外部js文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆