syntaxerror Posted September 3, 2007 Share Posted September 3, 2007 Hey everyone, I would just like to ask about the performance difference between maintaining javascript file and referencing to the .js versus running a php function that will 'write' the javascript into the final .php for the client? Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted September 3, 2007 Share Posted September 3, 2007 None. It's just (IMO) better to separate things (HTML, CSS and Javascript) though. Quote Link to comment Share on other sites More sharing options...
Azu Posted September 3, 2007 Share Posted September 3, 2007 Hey everyone, I would just like to ask about the performance difference between maintaining javascript file and referencing to the .js versus running a php function that will 'write' the javascript into the final .php for the client? Having an external javascript file is much better/faster then printing the javascript into the source code and having the client redownload it all on every request. Quote Link to comment Share on other sites More sharing options...
syntaxerror Posted September 4, 2007 Author Share Posted September 4, 2007 Yes, of course it's always standard practice to have a separate javascript file that you could place a reference but I just wanted to know the mechanics of how Apache might facilitate these requests comparatively... I meant between having a separate javascript file and just calling a php function that would 'print' the javascript to html Although security issues seem to have locked me down to using separate javascript file. Quote Link to comment Share on other sites More sharing options...
neylitalo Posted September 4, 2007 Share Posted September 4, 2007 but I just wanted to know the mechanics of how Apache might facilitate these requests comparatively... There are two minor differences. Embedded JS: 1) Apache only handles one request for the PHP file and the Javascript data. 2) JS file is run through PHP - a little overhead is incurred here, but nothing noteworthy. Separate JS file: 1) Apache handles two requests - one for the PHP file and one for the Javascript file. 2) JS file is not run through PHP. Apache gets the request, gets the file, and sends it. I hope that answers your question? Quote Link to comment Share on other sites More sharing options...
emehrkay Posted September 4, 2007 Share Posted September 4, 2007 yslow recommends that you make fewer http requests, even with external js files. if you can manage your js from php, id say do it, but it will be a lot more work than having a separate js file Quote Link to comment Share on other sites More sharing options...
emehrkay Posted September 4, 2007 Share Posted September 4, 2007 for example, in my templating engine i have methods designed to place inline js and it is kinda harder to write becuase in most cases I have to create a separate js file and initialize it in php. its an extra step, but it works for me. Quote Link to comment Share on other sites More sharing options...
Azu Posted September 4, 2007 Share Posted September 4, 2007 Yes, of course it's always standard practice to have a separate javascript file that you could place a reference but I just wanted to know the mechanics of how Apache might facilitate these requests comparatively... I meant between having a separate javascript file and just calling a php function that would 'print' the javascript to html Although security issues seem to have locked me down to using separate javascript file. Apache will handle it just like it would handle any other file request.. it's not going to go "HMMM.. this looks like JAVASCRIPT! So let's **** some **** up lol!". Quote Link to comment Share on other sites More sharing options...
syntaxerror Posted September 4, 2007 Author Share Posted September 4, 2007 well... this seems to give me a better picture of the situation, or maybe i was just looking for someone who'd back me up on the idea of making php function call that would write the javascript inline... the initial idea really was to avoid making the client create a separate request for the javascript... on the other hand, a colleague mentioned that if the same script were to be used multiple times - when a separate javascript file is maintained, the client makes only one request... and 'remembers' the code... so that the next time the page is called, it would no longer request for the same javascript - whilst if i were to let php handle writing the inline script, for every call on the same page, php would be processing the same function for each request. But thank you very much, guys, for some informative replies... i'm pretty new into php... and i've just crash-coursed into HTML/CSS/PHP/Javascript/Apache/MySQL and even Ajax, for about half a year(BOOM!) Quote Link to comment Share on other sites More sharing options...
emehrkay Posted September 4, 2007 Share Posted September 4, 2007 "- when a separate javascript file is maintained, the client makes only one request... and 'remembers' the code... so that the next time the page is called, it would no longer request for the same javascript" while true, doesnt that depend on your caching options? Quote Link to comment Share on other sites More sharing options...
syntaxerror Posted September 4, 2007 Author Share Posted September 4, 2007 well... good point... this might be something to consider on a larger environment but since i'm maintaining the system under an active directory controlled wan ... i'd probably still benefit from default cache settings 9 out of 10. (maybe) in the end, i guess i finally rested the idea of writing inline script for security reasons... just because it's so easy for someone to right click on the page and view the source script... Quote Link to comment Share on other sites More sharing options...
neylitalo Posted September 4, 2007 Share Posted September 4, 2007 in the end, i guess i finally rested the idea of writing inline script for security reasons... just because it's so easy for someone to right click on the page and view the source script... It's not much more difficult to right-click on the page, view the source, find the script's source, and navigate to it... Quote Link to comment Share on other sites More sharing options...
syntaxerror Posted September 5, 2007 Author Share Posted September 5, 2007 it's not a very bad single step delay at all... as opposed to someone incidentally right clicking on a page, clicking view source and... BAM!, the person stumbles into script right on top of the code, with all the familiar looking variables associated with the data i'm always assuming that over 75% of my users that might accidentally view the source code, won't even have an idea of how to extract the script via the reference... then again, are you guys hinting that I just maintain inline script anyway? i'm very open to input Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 5, 2007 Share Posted September 5, 2007 There are actually some ways to hide your JS code from really being viewed if you generate it with PHP. However, the only one I ever considered relies on what is sort of a glitch in php's session handling. (or something like that, I haven't played with it in forever) Quote Link to comment Share on other sites More sharing options...
syntaxerror Posted September 5, 2007 Author Share Posted September 5, 2007 Aha, I don't think I'm ready for that... But I suppose that would really, really be useful, specially when some Ajax code start piling up... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.