Jump to content

Recommended Posts

People always say that if you have JavaScript code which is used by multiple pages then it should be in an external page but if it is only used by a single page in should be internal. People also say that external JavaScript code can make the browsing slower since additional PHP requests are made but that when it is internal less bandwidth is used. Is it a good idea to put JavaScript code into PHP files and then include them like any other PHP files? I've been thinking about this because then I can minimise the HTTP requests and at the same time use the same JavaScript files in many PHP files.

Link to post
Share on other sites

I'm talking about storing the JavaScript code inside PHP files (.php) and including it in your HTML/PHP files (include "javascript/menu.php") so that the contents of it appear to belong to the HTML page itself.

Edited by LittleJoe
Link to post
Share on other sites

Someone else will need to address your specific speed question. IMO, any speed advantage would be minimal and probablly not noticable to users re: internal v external js.

Link to post
Share on other sites

You have maybe forgotten about caching. Once a user has downloaded a js file, it will stay cached, and subsequent requests won't be made. The browser will used the cached version instead. Back in the 90s, when everyone had crappy modems and servers lived on 512K routers instead of T3 routers, small bandwidth savings mattered. Today, even if we didn't have caching, a normal HTTP request is nothing to worry about. You won't save any noticeable time at all. Most of the latency I see these days comes from slow DNS look-ups and Google Analytics. YMMV

Link to post
Share on other sites
Most of the latency I see these days comes from slow DNS look-ups and Google Analytics. YMMV
And Facebook buttons. And Google +1 buttons. And Digg/Reddit/Yelp/any other third party buttons. And advertisements.
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...