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???
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
...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,
^_^
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...
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,
^_^
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.
Copy link to clipboard
Copied
That it's trash code so I kind of rewrote it... 😮
I'm not the creator of that code, just the janitor!
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,
^_^
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...