Jump to content

Placing a reference to javascript code versus running a php function


syntaxerror

Recommended Posts

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.
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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!".
Link to comment
Share on other sites

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!)

 

Link to comment
Share on other sites

well...  ;D 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...

 

 

 

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.