2012-08-08 153 views

回答

0

最後設計用於裝載HTML的解決方案。我已經處理了IE和NON-IE瀏覽器的情況。是這樣的 -

功能FNAME(){

if(navigator.appName == "Microsoft Internet Explorer") {   
    var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); 
    xmlHttp.open("GET", "templates.html", true); 

    xmlHttp.onreadystatechange = function() { 
     if(xmlHttp.readyState == 4 && (xmlHttp.status == 0 || xmlHttp.status == 200)) 
     {     
      document.getElementById("div_id_here").innerHTML = '"' + xmlHttp.responseText + '"'; 
     } 
    }; 
    xmlHttp.send(); 
} 

else { 
    var xmlHttp = new XMLHttpRequest(); 
    xmlHttp.open("GET", "templates.html", true); 
    xmlHttp.onreadystatechange = function() 
    { 
     if(xmlHttp.readyState == 4 && (xmlHttp.status == 0 || xmlHttp.status == 200)) 
     { 
      document.getElementById("div_id_here").innerHTML = xmlHttp.responseText; 
     } 
    }; 
    xmlHttp.send(); 
    } 
} 
+0

從長遠來看,您希望避免這樣的代碼,因爲它可能會非常快速地變成一團糟,因爲您最終不會重用任何代碼。你嘗試過使用'Y.io()'嗎?我的猜測是你的服務器沒有發送正確的「Content-Type」頭文件。這通常會破壞IE – juandopazo 2012-09-06 13:00:11

0

不爲$.load(...)一個確切的更換,但可能讓你在正確的方向開始:

加載跨域HTML文件是使用一個跨域的XMLHttpRequest或Flash的唯一途徑。上述這些問題都在YUI 3中的IO模塊的支持,但在YUI不支持2
來源:http://yuilibrary.com/forum/viewtopic.php?p=25704&sid=33da592b2224850ea738e6947d8dc280#p25704

+0

想知道一個簡單的XHR怎麼可以用來做這個。雖然XHR在IE中不能很好地用於這些任務。當然,無論瀏覽器如何,解決方案都必須工作。 – StackOverflow 2012-09-06 11:10:18

7

它需要一個特定的部分組件node-load,但它是相同的符號。

YUI().use('node', 'node-load', function (Y) { 
    Y.one('#templates').load('file1.html'); 
}); 

還有的在jQuery和YUI的所有方法一個很好的列表:http://www.jsrosettastone.com/

+0

這是正確的,但在IE中不起作用。 – StackOverflow 2012-09-06 10:21:26