0

I would like to put a jQuery function which contain php in my main.js page. How can I do ?

<script type="text/javascript">

    var r = parseInt('<?php the_field('red'); ?>'); 
    var g = parseInt('<?php the_field('green'); ?>'); 
    var b = parseInt('<?php the_field('blue'); ?>');


    $("body").css('background-color', 'rgb(' + r + ',' + g + ',' + b + ')');

        var alpha = Math.min($(this).scrollTop() / 2000, 1);
        var r = Math.round((230-parseInt('<?php the_field('red'); ?>')) * alpha + parseInt('<?php the_field('red'); ?>')); 
        /* red : (fin - début + début) */
        var g = Math.round((230-parseInt('<?php the_field('green'); ?>')) * alpha + parseInt('<?php the_field('green'); ?>')); 
        /* red : (fin - début + début) */
        var b = Math.round((230-parseInt('<?php the_field('blue'); ?>')) * alpha + parseInt('<?php the_field('blue'); ?>')); 

   $(document).scroll(function() {
        /* red : (fin - début + début) */
        $("body").css('background-color', 'rgb(' + r + ',' + g + ',' + b + ')');
    });

</script>
5
  • You can not put php on core js file.(main.js). Commented Mar 6, 2017 at 15:54
  • What error do you get? Commented Mar 6, 2017 at 15:56
  • 2
    You won't be able to execute the PHP code within your JavaScript because that's purely client side and can't execute on the server, but you can use AJAX or similar to call a PHP page and have the results returned. Commented Mar 6, 2017 at 15:57
  • The <script> element implies that this isn't actually a JavaScript file, but is an HTML file. Regardless, you can put PHP code in anything that has a .php extension and/or is processed by the PHP engine server-side. What have you tried and in what way is it not working? Commented Mar 6, 2017 at 15:58
  • 1
    @user3870112: If you want to clarify that question, do so on that question. If there's information you'd like to add to this question, do so on this question. Stack Overflow isn't a scavenger hunt. As for this question, it's not really clear to me what the problem is here. What does the code in the question become client-side? In what way is this not working? Commented Mar 6, 2017 at 16:02

1 Answer 1

1

You can't. PHP is processed in the backend by the PHP server, while JS is processed in the frontend by the browser (JS can be processed in the backend also with NodeJS, but that's a different topic)

Now, to accomplish what you are trying to do, create a <script> tag in your header before you load main.js, in which you add a global var that contains the colors:

<script type="text/javascript">
  var _COLORS_ = {
        r: parseInt('<?php the_field('red'); ?>'), 
        g: parseInt('<?php the_field('green'); ?>'),
        b: parseInt('<?php the_field('blue'); ?>')
  };
</script>

Then, in your main.js file, read from that variable: _COLORS.r or _COLORS.g, or _COLORS.b.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.