Including a javascript file using javascript

Engaged ,
Jun 24, 2016 Jun 24, 2016

Copy link to clipboard

Copied

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

Views

886

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct Answer

LEGEND , Jun 24, 2016 Jun 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 worki...

Likes

Translate

Translate
LEGEND ,
Jun 24, 2016 Jun 24, 2016

Copy link to clipboard

Copied

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,

^_^

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jun 24, 2016 Jun 24, 2016

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 24, 2016 Jun 24, 2016

Copy link to clipboard

Copied

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,

^_^

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jun 30, 2016 Jun 30, 2016

Copy link to clipboard

Copied

WolfShade wrote:

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

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

I think that this did it:

WolfShade wrote:

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jul 15, 2016 Jul 15, 2016

Copy link to clipboard

Copied

LATEST

That it's trash code so I kind of rewrote it...  😮

I'm not the creator of that code, just the janitor! 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 24, 2016 Jun 24, 2016

Copy link to clipboard

Copied

PS.. you can also use JavaScript to load a .js file; but it won't be synchronous, so you'll never know if/when the included JavaScript is loaded/executed.

V/r,

^_^

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jun 27, 2016 Jun 27, 2016

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines