Friday, August 29, 2014

Don't Make Convoluted Code Part 2

Client-Server Processes
Client-Server Processes
Now we have enough understanding of the differences between client-side and server-side scripting and the scenario of the entire process with the help of previous part.


Let's explore the real life scenario through an example code where PHP code has messed up with the JavaScript code in a website programming document.

<script type="text/javascript">
    var foo = 'bar';
    <?php
        file_put_contents('foo.txt', ' + foo + ');
    ?>

    var baz = <?php echo 42; ?>;
    alert(baz);
</script>

our hire dedicated programmer team has created and run this test code and unfortunately browser has responded in wired way. It displayed '42' placed into the PHP code, instead of 'bar' put into the JavaScript code.


Let's dissect the code in to two fragments. One is JavaScript code and another is PHP code placed as nested code in JavaScript code. As we know that JavaScript code is falling in to the client-side code category. Whereas PHP code is purely server-side code. I am using word 'purely' as PHP never runs on the client devices at all while JavaScript code may created as the ensue of server-side scripting and server many times send JavaScript code embedding into the HTML documents to get desired results. Therefore, we can't say that JavaScript code is not found on the server. However, server don't need to run JS in most cases and simply generate it or packed with HTML docs.


With this understanding let's see what happened with our example code closely. When we run this code to test, it reaches to the server and run the PHP code first in the server. Once the PHP code execution finishing or PHP scripts end up, server stops the running any code and return the results obtained through PHP code compilations. There are no chances of running JavaScript on the server at all. Moreover, the returning results also missing the original JavaScript code so you only getting the results of PHP code executions in the browsers of the clients.


Therefore, in our example code we don't get any results for the 'foo' variable written in JavaScript. Whereas server run the part of script located in between the <?php...?> tags only and send alert 42 in return. Therefore, only alert call works and foo variable missing at all. However, this was a simple example code, but when you need to pass the variable or any sort of data written in between the JavaScript there is a programming trick.


You have to rely on the Ajax code means you have to call server via Ajax requests for any kind of dynamic data or you have to put data into the source code of the page, if they are static data so it can be processed with JavaScript. The later technique of embedding in to JavaScript is bit complicated and demands adequate knowledge of converting data into the server-side scripting for further processing.


Conclusively, I can say that if you have any ambiguity regarding to the server-side and client-side scripting and their rules of using in practice, you will end up with convoluted code and prove your code a headache for QA team or for the maintenance team in future. Therefore, our hire dedicated developers department take care of such code understanding and best coding practices prevailing in the industry in order to provide quality work to our patrons.


Total Pageviews

Pages