2010-05-30 16:29 阅读 24

Jquery函数。 我应该把它放在一个文件中还是可以散布它?

So, I have a lot of php files where I use jquery functions. Do I need to create a seperate .js file for all jquery functions or I can put it together with php files or it actually doesn't matter at all?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

3条回答 默认 最新

  • 已采纳
    duanpu5048 duanpu5048 2010-05-30 16:36

    If you need a custom function on a page, I would leave that in the page. Anything your going to reuse between pages, should be in a global library function that you can include on all pages. Try to reuse as much as your code as you can in a library function.

    Try to extract as much as your code into library functions. It's easier to maintain, and reduces code duplication.

    点赞 评论 复制链接分享
  • duanqiang6501 duanqiang6501 2010-05-30 16:36

    If you are using a function in more than one places, then it is beneficial (more maintainable) to put it in an external file and include that wherever you need to.

    点赞 评论 复制链接分享
  • douhan0562 douhan0562 2010-05-30 18:59

    Another advantage of keeping your Javascript external is that the browser can cache the files, even if it isn't caching the pages of your php application. By cutting down the page size you're making the site faster.

    A downside of keeping Javascript external is that you can't embed php directives in the code itself. That can sometimes be a problem, though there are ways of arranging for your Javascript code to be able to "discover" things in the page.

    点赞 评论 复制链接分享