PHP In Javascript via AJAX & Async

To my understanding AJAX makes javascript asynchronous. I am new to AJAX, but noticed when creating scripts to the html document there is an attribute for async for scripts. Do these do the same thing as AJAX or am I wishfully thinking. I would find it very useful to use php within my already existing javascript file used in DOM generation. Why? To make life easier and as OO as possible. The only real thing I want out of this is to use php to output my php file generated from javascript, but later I would like to generate php directly using javascript methods.


 <!DOCTYPE html>

<script type="text/javascript" src="jWebKit.js"></script>


     var div = new Div();



    var head;
var body;
var jScript;
var devScript;
var phpScript;


    jScript = document.createElement("script");
    jScript.src = "jWebKit.js";
    jScript.type = "text/javascript";

    devScript = document.createElement("script");

    phpScript = document.createElement("script");
    php.type = "text/javascript";
    php.text = 'document.write("<?php fopen("testfile.php", "w") ;?>");'; // This is the target script needed for file output below...
    phpScript.async = 'true';


window.onload = function(){

    var cutScript;

    head = document.head;
    body = document.body;

    cutScript = head.innerHTML.toString().replace(jScript.outerHTML.toString(),'');

    devScript.text = phpScript.innerHTML.toString() + cutScript.replace('<script>', '').replace('</script>','');//Does not work!

    head.innerHTML = head.innerHTML.toString().replace(cutScript,'');




function Div(){

    Div.STATIC = 'static';
    Div.ABSOLUTE = 'absolute';
    Div.RELATIVE = 'relative';
    Div.FIXED = 'fixed';
    Div.SOLID = 'solid';
    Div.DOTTED = 'dotted';
    Div.LEFT = 0;
    Div.CENTER = 1;
    Div.RIGHT = 2;
    Div.TOP = 0;
    Div.MIDDLE = 1;
    Div.BOTTOM = 2;

    var ELEMENT;
    var CSS;

    var horizontalAlign;
    var verticalAlign;

    var colorQueue;

    (function() {

        this.div = document.createElement('div');

        ELEMENT = this.div;
        CSS =;

        CSS.border = '1px solid black';



    this.setPosition = function(postype){

        if(!horizontalAlign && !verticalAlign){

            CSS.position = postype;



    this.setBounds = function(x,y,width,height){

        CSS.left = x + 'px'; = y + 'px';
        CSS.width = width + 'px';
        CSS.height = height + 'px';


    this.setColorQueue = function(r,g,b){

        colorQueue = 'rgb(' + new Array(r,g,b) + ')';


    this.horizontalAlign = function(horiz){

        var freeSpaceX = ((window.innerWidth - ELEMENT.offsetWidth) / 2);
        var defPadding = '8px';
        var defPaddingCenter;
        var defPaddingRight;
        var defPaddingLeft;

        horizontalAlign = true;


        if(CSS.position == 'relative' || CSS.position == 'absolute'){

            CSS.position = 'absolute';
            defPaddingCenter = 12;
            defPaddingRight = 4;
            defPaddingLeft = 8;

        }else if(CSS.position == 'fixed'){

            defPaddingCenter = 4;
            defPaddingRight = 4;
            defPaddingLeft = 8;


        if(horiz == 0){

                CSS.marginTop = defPadding;
            }CSS.marginLeft = defPaddingLeft + 'px';

        }else if(horiz == 1){

                CSS.marginTop = defPadding;
            }CSS.marginLeft = freeSpaceX - defPaddingCenter + 'px';

        }else if(horiz == 2){

                CSS.marginTop = defPadding;
            }CSS.marginLeft = (freeSpaceX - defPaddingRight) * 2 + 'px';





I don't know what exactly you want to do, but you are creating a php script that never go to the server, for that reason will never execute (only the server understand php script) if you wanna call the url testfile.php the you should do something like this

xmlhttp = new XMLHttpRequest();"GET","testfile.php?q=something",true);

if (xmlhttp.readyState==4 && xmlhttp.status==200)
  //the responseText have the server response

see this site for more info

See the next chapter too on the link

By the way, jquery will help you alot with all

Need Your Help

postioning elements using percentage

jquery css css3

Hi all am trying to position some elements using percentage which are defined as position:absolute but the position are collapsed with each other on different screens because of percentages...

Python: select() returns immediately without no data to read (telnetlib)

python python-2.7 telnet telnetlib

I want to use to block the loop until some data is ready to be read from a list of telnet sessions. However, the below code does not work and the call to returns

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.