Is it possible to run a JavaScript function through PHP?

4

I want to run a function of JavaScript without "submitting" it, that is when the screen is accessed the php will call the function JavaScript and run what it has in it and then continue the normal code. It's possible? Note: The function contains data that only runs in JavaScript !

My goal is to run this section:

<script language="javascript" type="text/javascript">
    for(i = 0; i < 10; i++){
        if(objp!=''){           
            n_p[i] = 'P' + objp[0];
            objp.splice(0,1);
        }
        else{
            n_p[i] = 'S' + objs[0];
            objs.splice(0,1);
        }   
    }
</script>
//No PHP

For a better understanding of the above section follow Here

    
asked by anonymous 13.10.2014 / 15:24

2 answers

12

PHP does not know JavaScript

PHP can not directly call a JavaScript function because PHP is interpreted on the server side.

PHP does not see a code there, it simply copies the character-by-character text and sends it to the user's browser.

The browser knows JavaScript

When the browser reads the data already received in HTML, it starts interpreting this HTML and assembles the elements on the screen.

When encountering a <script> tag the browser stops what it is doing and executes what it has in that script.

Note that at this point the browser is running the script on the user's computer, while PHP (which may have already finished running) was running on the server.

Run code on page load

So, for every effect, the code quoted in the question will run in the browser exactly the moment the browser receives it from the server. An explicit call is not required.

<script language="javascript" type="text/javascript">
    //todo código aqui será executado
</script>

Performing functions

However, in the case of having a declared function, the excerpt is interpreted and the function is created, but it is not invoked automatically:

<script language="javascript" type="text/javascript">
    function f_mostra() {
        alert("Entrei");
    }
</script>

However, just add a call after the function declaration to execute it:

<script language="javascript" type="text/javascript">
    function f_mostra() {
        alert("Entrei");
    }
    f_mostra();
</script>

Or:

<script language="javascript" type="text/javascript">
    function f_mostra() {
        alert("Entrei");
    }
</script>
<!-- ... -->
<script language="javascript" type="text/javascript">
    f_mostra();
</script>

Waiting for the page to be ready

The problem is that it is not always desirable to run JavaScript while the page is loading, after all the browser may not have finished putting all the HTML in yet.

You can then capture the event that says if the page is fully loaded :

window.onload = function() {
  //executado quando tudo na página está carregado
};

Everything I said above is not exactly true;)

There are techniques and technologies that actually allow PHP (or any other language) to execute a JavaScript function when the user is with an open page, without a new request.

Today with HTML 5, we have WebSockets technology that enables persistent communication between client (JavaScript) and server (PHP). However, I will not go into detail, as it is a more complex subject.

    
14.10.2014 / 18:29
-3

It's very simple, just call the JavaScript function by echo in PHP.

echo '<script>funcaophp();</script>'
    
02.07.2018 / 15:27