Skip to main content
Inspiring
June 24, 2016
Answered

Including a javascript file using javascript

  • June 24, 2016
  • 2 replies
  • 2410 views

Just playing around with including javascript templates into my CF code when I came across this issue.

I have two files I'm testing with.  The javascript file I want to include is called KDL.js.  The contents are listed here:

<script language="javascript">

  function KDL_Test(MyString) {

  var MsgString = MyString + "KDL Rocks";

  alert(MsgString);

  }

</script>

The other template is called KDL.cfm, and it's contents:

<CFINCLUDE TEMPLATE="kdl.js">

<script language="javascript">

  KDL_Test("I am here. ");

</script>

This works fine as I get my alert window whenever I execute the code.

What I don't understand is why doesn't the javascript alert window get called when I use javascript to include the javascript template?

<script language="JavaScript" type="text/javascript" src="kdl.js"></script>

<script language="javascript">

  KDL_Test("I am here. ");

</script>

This code doesn't generate any error messages but I don't get the popup window either like I did with the first include.  Thoughts???

    This topic has been closed for replies.
    Correct answer WolfShade

    language="JavaScript" has been deprecated for years, and should not be used.  It has been replaced with type="text/javascrpt", which you already have.  And as long as the .js file is in the same folder as the .cfm file, it should work.

    I've never seen anyone CFINCLUDE a .js file, before, so I'm baffled.  It really shouldn't work, seeing as how JavaScript is a client-side language, and CF is a server-side.  But, if it works, I can't argue it (even though my brain cringes at the thought of it working.)

    Another thing:  Any JavaScript code contained in a .js file should not have <script></script> tags.  It is assumed script since it is inside a file with a .js extension.

    Load the .cfm file into a browser with Developer Tools on (F12).  If you are using FireFox with the FireBug plugin, you should see a great deal of information as to why it isn't working.

    HTH,

    ^_^

    2 replies

    Inspiring
    June 27, 2016

    I just threw it into a CFINCLUDE tag. Still not sure why they did it that way... then again I'm having troubles wrapping my head around the next question I'm about to ask the forum too... 

    WolfShade
    WolfShadeCorrect answer
    Legend
    June 24, 2016

    language="JavaScript" has been deprecated for years, and should not be used.  It has been replaced with type="text/javascrpt", which you already have.  And as long as the .js file is in the same folder as the .cfm file, it should work.

    I've never seen anyone CFINCLUDE a .js file, before, so I'm baffled.  It really shouldn't work, seeing as how JavaScript is a client-side language, and CF is a server-side.  But, if it works, I can't argue it (even though my brain cringes at the thought of it working.)

    Another thing:  Any JavaScript code contained in a .js file should not have <script></script> tags.  It is assumed script since it is inside a file with a .js extension.

    Load the .cfm file into a browser with Developer Tools on (F12).  If you are using FireFox with the FireBug plugin, you should see a great deal of information as to why it isn't working.

    HTH,

    ^_^

    Inspiring
    June 24, 2016

    Not my code...  I just have to maintain it and figure out why some things don't work... of which this was one.  I wish I had carte blanche to make changes but alas, I don't...

    WolfShade
    Legend
    June 24, 2016

    Yeah, like the old saying:  "Mine is not to wonder why.  Mine is but to code and die."

    Thank you for marking my answer as correct.  I do appreciate it.

    Just out of curiosity - which fix was the right one?

    V/r,

    ^_^