2014-06-29 47 views
0

據我瞭解,AJAX使JavaScript異步。我是AJAX的新手,但在爲html文檔創建腳本時注意到了腳本的異步屬性。它們是否和AJAX做同樣的事情,或者我是否想過。我會發現它非常有用在我已有的用於DOM生成的JavaScript文件中使用php。爲什麼?儘可能讓生活更簡單,並儘可能做到面向對象。我想要的唯一真正的東西是使用PHP來輸出我從JavaScript生成的PHP文件,但後來我想直接使用JavaScript方法生成PHP。PHP在JavaScript中通過AJAX和異步

的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'; 

     } 

    } 

} 
+1

不能產生很多從JavaScript的PHP,你必須明白,PHP是服務器端和JavaScript是客戶端。 – stalin

+0

@stalin然後我誤導了http://www.hotscripts.com/forums/javascript/39278-php-inside-javascript.html。 – StoneAgeCoder

+0

顯然你做到了,請參閱評論#3'JavaScript無法直接執行服務器端代碼' – stalin

回答

2

我不知道你想做什麼,而是要創建一個PHP腳本,從來沒有去到服務器,由於這個原因將永遠不會執行(只有服務器理解PHP腳本)如果你想打電話的網址testfile.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將幫助您解決所有

+0

感謝您的幫助。如果有什麼我現在瞭解XML和AJAX更:)。 – StoneAgeCoder