據我瞭解,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';
}
}
}
不能產生很多從JavaScript的PHP,你必須明白,PHP是服務器端和JavaScript是客戶端。 – stalin
@stalin然後我誤導了http://www.hotscripts.com/forums/javascript/39278-php-inside-javascript.html。 – StoneAgeCoder
顯然你做到了,請參閱評論#3'JavaScript無法直接執行服務器端代碼' – stalin