JavaScript CFML within Javascript

I am working on code written by someone else. To my knowledge its not correct but I am not seeing any errors and so now I am not sure if something changed since version 8.

The code is as below & I want to know if you can use CFML with Javascript code

<script language="JavaScript" type="text/JavaScript" > 
    var x = 2;
    <cfif url.new = true>var x = 5 </cfif>
</script>
Answer:1

You cannot use CFML within JavaScript per-se because ColdFusion runs on a server and JavaScript runs on the client. However, you need to consider how CFML works, the CFML code is processed on the server and then the output is passed on to the client. So in your case, the example that you provided is valid. The CFML will be processed on the server and one of two outputs will be generated and passed back to the client.

NOTE: You haven not included all of the code so I am assuming that there is additional code before this that checks for the existence of the url.new variable. Otherwise if the new argument is not passed in the URL then an error will be thrown that the variable does not exist.

If the URL does not contain an argument named "new"; http://www.yourdomain.com/somepage
or the "new" argument does exist but it's value is not considered to be "true"; http://www.yourdomain.com/somepage?new=false
then the output will be:

<script language="JavaScript" type="text/JavaScript" > 
    var x = 2;
</script>

Note that the value of "x" in the generated JavaScript will be 2.

If the URL does contain an argument named "new" and it's value is considered to be "true"; http://www.yourdomain.com/somepage?new=true
then the output will be:

<script language="JavaScript" type="text/JavaScript" > 
    var x = 2;
    var x = 5 
</script>

Note that the value of "x" in the generated JavaScript will be 5, however there is also a missing semi-colon at the end of the line so your example may throw a JavaScript error.

Notice that I also mention "if the value is considered to be true or false". In ColdFusion, Boolean expressions, True, nonzero numbers, and the strings "Yes", "1|", "True" are equivalent; and False, 0, and the strings "No", "0", and "False" are equivalent. Boolean evaluation is not case-sensitive. For example, True, TRUE, and true are equivalent. So in your example, http://www.yourdomain.com/somepage?new=1 would also be considered true.

Answer:2

Well: you're not really using "CFML with Javascript code" in the way you're suggesting. What you have is some text, and CFML happens to be (conditionally) generating some other text.

The text it generates is going to be one of:

<script language="JavaScript" type="text/JavaScript" > 
    var x = 2;
    var x = 5 
</script>

Or

<script language="JavaScript" type="text/JavaScript" > 
    var x = 2;

</script>

Depending on the value URL.new. Either way, it's all just text at the point the CFML server has any input into it.

That text then gets sent back to the web server, which sends it back to the browser which requested it. The browser then interprets the text as HTML, and within the HTML is some text which it's been told to treat as JavaScript. And so it does do (treats it as JavaScript, and runs it).

But this is long since the CFML server has had anything to do with it. All the CFML server does it churn out text.

This might help your understanding of the request/response lifecycle vis-a-vis a CFML server: "The CFML request/response process"

Answer:3

I am using AngularJS with MVC file structure. I have a data structure in my controller that I want to display on my view. I have a custom "indicators" directive. Each "title" belongs to an indicator ...

I am using AngularJS with MVC file structure. I have a data structure in my controller that I want to display on my view. I have a custom "indicators" directive. Each "title" belongs to an indicator ...

I want to put my JSON data into Vue data, and a display, why can't I get to work? compiled: function(){ var self = this; console.log('teste'); $.ajax({ url: 'js/fake-ws.json', ...

I want to put my JSON data into Vue data, and a display, why can't I get to work? compiled: function(){ var self = this; console.log('teste'); $.ajax({ url: 'js/fake-ws.json', ...

  1. putting json data into html table
  2. putting json in database
  3. putting data into json object

Let's say I have a simple array like this: var myArr = [0,1,2,3,4,5,6,7,8,9] I'd like to extract a number of elements, starting from a specific index, like this: myArr.getElementsFromIndex(index, ...

Let's say I have a simple array like this: var myArr = [0,1,2,3,4,5,6,7,8,9] I'd like to extract a number of elements, starting from a specific index, like this: myArr.getElementsFromIndex(index, ...

  1. number elements from
  2. number elements in periodic table
  3. number elements in list python
  4. number elements in array python
  5. number elements in array matlab
  6. number elements in array javascript
  7. number elements in list r
  8. number elements in array c++
  9. number elements in numpy array
  10. number elements in array
  11. number elements in array perl
  12. number elements in each period
  13. number elements in a group
  14. number elements of matrix
  15. number of elements matlab
  16. number of elements python
  17. number of elements r
  18. number of elements numpy
  19. elements number of protons
  20. number of elements less than k

I'm very new to JavaScript (and coding) and I'm studying through the book Head First JavaScript. From reading, I thought an anonymous function could be an argument since functions are values, but I ...

I'm very new to JavaScript (and coding) and I'm studying through the book Head First JavaScript. From reading, I thought an anonymous function could be an argument since functions are values, but I ...