开发者

Jquery functions. Should I hold it in one file or is it okay to intersperse it?

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 o开发者_开发问答r I can put it together with php files or it actually doesn't matter at all?


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜