SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2010
    Location
    Canada, Alberta
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question external javascript with php question

    just out of curiosity, what are the disadvantages of calling an external JavaScript file but having the file type as .php

    ex

    Code:
    <script type="text/javascript" src="example.php"></script>
    Since I can just put something like
    Code:
    Header("content-type: application/x-javascript");
    to have it render as JavaScript.

    Is this a "good" or "bad" practice?

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,191
    Mentioned
    191 Post(s)
    Tagged
    2 Thread(s)
    AFAIK browsers won't balk at it. The only possible problem I can think of is that some may be able to see that its a generated file and maybe exploit the fact?

    Of course you could do some htaccess rewrite stuff so that requests for myphpjavascriptfile.js go to myphpjavascriptfile.php and nobody would be the wiser.

    You may want to use some expire nocache headers too.

  3. #3
    SitePoint Enthusiast
    Join Date
    Dec 2010
    Location
    Canada, Alberta
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cool, I just really dislike it when I have to put the actual script on the page it self >_<. So if I avoid using http queries like $_GET I should be fine?

    Thanks Mittineague

  4. #4
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,191
    Mentioned
    191 Post(s)
    Tagged
    2 Thread(s)
    I've used both $_GET and $_POST to fetch JSON without any problems. Just be careful to validate and sanitize as should be the case always anyway.

  5. #5
    SitePoint Guru aamonkey's Avatar
    Join Date
    Sep 2004
    Location
    kansas
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I do a lot of my CSS and js includes that way so I can use php variables inside the scripts or stylesheets. No measurable downside.
    aaron-fisher.com - PHP articles and more


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •