2010-10-30 24 views
0

我想用我自己的代碼,在http://wtikay.com/和各種其他地方看到的歷史嗅探演示覆制。 (這是一個很長的故事。)複製wtikay - IE瀏覽器 - currentStyle不更新時,它應該

我有一些在大多數老版本的瀏覽器(最新的Safari和Chrome版本和Firefox 4 beta版都有防禦功能)可靠地工作 - 但它在IE7或8(haven'試着6),我不明白爲什麼。這似乎是,IE只是不打擾更新其渲染,當我改變a元素href財產 - 但據我所知,這正是wtikay所做的,它的工作原理。

測試文件:

<!doctype html> 
<html><head> 
<title>gevalt</title> 
<style> 
    body { background-color: gray } 
    a:link { text-decoration: none; color: black } 
    a:visited { color: white } 
</style> 
<body> 
<span id="container"><a href="" id="testlink">test</a></span> 
<a href="ftp://">minus</a> 
<a href="http://www.google.com/">plus</a> 
<script> 
window.onload = function() 
{ 
    var testlink = document.getElementById("testlink"); 
    var results = document.getElementById("results"); 
    var container = document.getElementById("container"); 
    var report = ""; 

    testlink.href = "ftp://"; 
    container.removeChild(testlink); 
    container.appendChild(testlink); 
    report += " -" + (testlink.currentStyle || window.getComputedStyle(testlink, "")).color; 

    testlink.href = "http://www.google.com/"; 
    container.removeChild(testlink); 
    container.appendChild(testlink); 
    report += " +" + (testlink.currentStyle || window.getComputedStyle(testlink, "")).color; 

    results.appendChild(document.createTextNode(report)); 
}; 
</script> 
<pre id="results"> 
</pre> 
</body></html> 

在它的作品中,「測試」和「加」將是白色的(假設你已經在瀏覽器中訪問www.google.com),字瀏覽器「減號」將是黑色的,它將在下面打印類似「-rgb(0,0,0) +rgb(255,255,255)」的內容。在IE中,「測試」將是黑色而不是白色,並且在它下面將顯示「-black +black」。或者可能「測試」將是白色的,但在它下面將顯示「-white +white」。這兩個都是失敗的。

任何幫助將不勝感激。

回答

0

爲了記錄在案:要在IE這項工作,你有一個完全新的,你想讓它注意到在href的變化,每次和新元素來取代<a>元素必須被添加到文件。

這是6,7和8的情況;我還沒有嘗試v9 beta版。

window.onload = function() 
{ 
    var testlink; 
    var results = document.getElementById("results"); 
    var container = document.getElementById("container"); 
    var report = ""; 

    testlink = document.createElement("a");  
    testlink.href = "ftp://"; 
    container.appendChild(testlink); 
    report += " -" + (testlink.currentStyle || window.getComputedStyle(testlink, "")).color; 

    container.removeChild(testlink); 
    testlink = document.createElement("a"); 
    testlink.href = "http://www.google.com/"; 
    container.appendChild(testlink); 
    report += " +" + (testlink.currentStyle || window.getComputedStyle(testlink, "")).color; 

    results.appendChild(document.createTextNode(report)); 
};