javascript - PHP 通过 AJAX 和 Async 在 Javascript 中

标签 javascript php jquery ajax asynchronous

据我了解,AJAX 使 javascript 异步。我是 AJAX 新手,但注意到在 html 文档创建脚本时有一个脚本异步属性。这些与 AJAX 做同样的事情还是我一厢情愿。我发现在 DOM 生成中使用的现有 javascript 文件中使用 php 非常有用。为什么?让生活变得更轻松并且尽可能面向对象。我唯一想要的就是使用 php 输出由 javascript 生成的 php 文件,但后来我想直接使用 javascript 方法生成 php。

index.php

 <!DOCTYPE html>

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

<script>

     var div = new Div();
     div.setPosition(Div.FIXED);
     div.setBounds(100,0,100,100);
     div.horizontalAlign(Div.LEFT);
     div.setPosition(Div.RELATIVE);

 </script>

jWebKit.js

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

(function(){

    document.open();

    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!

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



    alert(document.documentElement.outerHTML);

    document.close();


};

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 = this.div.style;

        CSS.border = '1px solid black';

        document.body.appendChild(this.div);

    }());

    this.setPosition = function(postype){

        if(!horizontalAlign && !verticalAlign){

            CSS.position = postype;

        }


    }

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

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

    }

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

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

    }

    this.horizontalAlign = function(horiz){

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

        horizontalAlign = true;

        this.setBounds(0,0,100,100);

        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){

            if(!verticalAlign){
                CSS.marginTop = defPadding;
            }CSS.marginLeft = defPaddingLeft + 'px';

        }else if(horiz == 1){

            if(!verticalAlign){
                CSS.marginTop = defPadding;
            }CSS.marginLeft = freeSpaceX - defPaddingCenter + 'px';

        }else if(horiz == 2){

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

        }

    }

}

最佳答案

我不知道你到底想做什么,但是你正在创建一个永远不会访问服务器的php脚本,因此如果你想调用url testfile.php,它永远不会执行(只有服务器理解php脚本)你应该做这样的事情

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

xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
  //the responseText have the server response
  document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}

请访问此网站了解更多信息

http://www.w3schools.com/ajax/ajax_xmlhttprequest_create.asp

也可以通过链接查看下一章

顺便说一句,jquery 会为你提供很多帮助

关于javascript - PHP 通过 AJAX 和 Async 在 Javascript 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24480934/

相关文章:

php - 显示mysql中的特定行

jquery - 使用 jQuery Flot 绘制外部数据

javascript - 如何从 iframe 打开页面内部并将 nav-active 类添加到相应的页面菜单

javascript - 如何在 highcharts gauge-solid 中禁用颜色过渡

javascript - 单击链接时屏幕变暗然后重定向

javascript - Node.js:在写入之前检查文件是否存在

php - 使用 PHP 从字符串或文件中提取多个电话号码

javascript - 使用 JavaScript 动态添加/删除表行

php - php邮件中的抄送不起作用

jquery - 使用 jQueryRotate 旋转后图像变大了一点