Skip to main content
June 1, 2011
Answered

Call an external .js script with cf

  • June 1, 2011
  • 4 replies
  • 5041 views

I have seen several sites use a script declaration at the top of their page to an external file with a .php extension.  I did a little research and found that they are just calling a .js file with that php page so the contents of the .js is not viewable.

Can you do the same thing with coldfusion?

    This topic has been closed for replies.
    Correct answer 12Robots

    Adam, you are absolutely correct.  I used IE's view source function and like you said, the contents of my .cfm (which is really javascript) was all inline.

    That's heading down the wrong path.

    Again, I just want to do the same thing that php is doing here http://www.javascriptkit.com/javatutors/externalphp.shtml with coldfusion instead.


    I think you misunderstood Adam.  The JS generated by the PHP is also viewable. You cannot hide JavaScript. If it needs to execute on the client, then the end user will be able to see it. Period.

    4 replies

    June 1, 2011

    Adam and 12robots... sorry for wasting your time.  You guys are correct.

    I did more research and found a lengthy thread about client side javascript... and what I've determined is that the code is viewable no matter what.

    It seems like there are a lot of methods that developers use to delay the view/use of their javascript but the end result always seems the same.

    I will go back and try the method 12robots suggests and see if that gives me any piece of mind.

    Thanks everyone.

    12Robots
    Participating Frequently
    June 1, 2011

    These methods of creating the JS server-side are not to hide anythign, they give you the ability to create the JS code dynamically using variables from the server. It is very powerful and useful funcitonality, but it is not there for security.

    If you do not need to generate the JS server-side, then there is no point to doing it.

    Any code that needs to be secure *MUST* be on the server. 

    June 1, 2011

    Well, I wanted to give what 12robots suggested a try and I can't seem to get it right.

    Here is the .cfm include that I want to do the same as a .js would if I called it directly with the <script src

    This is called was_js_once.cfm...

    <cfheader name="Content-Type" value="application/x-javascript; charset=ISO-8859-1">
    var i=0;

    And my main.cfm page...

    <html>

    <head>

    <cfinclude template="was_js_once.cfm">

    </head>

    <body>

    Stuff

    </body>

    </html>


    When I try to run the main.cfm page it opens a dialog box and asks if I want to open or save main.cfm.

    WolfShade
    Legend
    June 1, 2011

    js.cfm:

    <script type="text/javascript">

    function doThis() {

       blah blah blah

       }

    function doThat(param1) {

       blah blah with param1

       }

    function wiffleBallBat() {

       you get the point

       }

    </script>

    index.cfm:

    <html> <!--- don't forget to declare your doctype!  --->

      <head>

        <title>test</title>

        <cfinclude template="js.cfm">

      </head>

      <body>

          yadda yadda yadda

      </body>

    </html>

    The nice thing about doing it this way is that you can use CF variables in the JavaScript, CFLOOP, CFOUTPUT, etc.

    Inspiring
    June 1, 2011

    How does that work?  The JS has to be delivered to the browser as JavaScript, so irrespective of what the URL is, it has to deliver JavaScript.

    What's an example of one of these URLs?

    To answer your question, yeah of course you can do it with CF.  The source URL for a SCRIPT tag can be anything you like, as long as it returns JavaScript.

    --

    Adam

    June 1, 2011

    Hi Adam.  Here is the page that explains the php method...

    http://www.javascriptkit.com/javatutors/externalphp.shtml

    Inspiring
    June 1, 2011

    In what way does that make "the contents of the .js [...] not viewable" ?

    --

    Adam

    Participating Frequently
    June 1, 2011

    Yes, a JS file can be named with a .cfm extension and included as such in a

    <script type="text/javascript" tag

    June 1, 2011

    I thought this may be the approach... however it did not work.  I took the contents of the original .js, saved it as .cfm,

    and then called it using <script type="text/javascript" src="myjsfile.cfm"></script>

    It did not work.

    Inspiring
    June 1, 2011

    What do you mean by "did not work"?

    --

    Adam