2013-08-29 22 views
0

我擁有這些CODE#1腳本,並且第一個腳本的源代碼正在Web服務上運行。我需要嘗試捕獲,所以當Web服務進入一個站點時,我可以像CODE#2那樣創建其他條件。我怎樣才能做到這一點。由於使用SRC添加嘗試使用JavaScript獲取內容

CODE#1

<script type="text/javascript" language="Javascript" src="http://gd.geobytes.com/gd?after=-1&variables=GeobytesCountry,GeobytesCity"> 
</script> 
<script type="text/javascript" language="Javascript"> 
document.write("<p>Welcome to visitors from "+sGeobytesCity+", " + sGeobytesCountry); 
</script> 

CODE#2

TRY 
{ 
<script type="text/javascript" language="Javascript" src="http://gd.geobytes.com/gd?after=-1&variables=GeobytesCountry,GeobytesCity"> 
</script> 
<script type="text/javascript" language="Javascript"> 
document.write("<p>Welcome to visitors from "+sGeobytesCity+", " + sGeobytesCountry); 
</script> 
} 
catch 
{ 
window.location = "geobytes.aspx"; 
} 

回答

2

不能使用的try/catch這樣。 (特別是因爲你把它放在腳本元素之外,所以它會成爲HTML代碼的一部分,而HTML不是編程語言,並且不知道它是try/catch構造。)

但是,如果第一個腳本應該創建你的第二個腳本試圖輸出變量sGeobytesCitysGeobytesCountry,你可以檢查它們是否在你的第二個腳本存在或不是第一:

if(typeof sGeobytesCity !== "undefined") { 
    document.write("<p>Welcome to visitors from "+sGeobytesCity+", " + 
    sGeobytesCountry); 
} 
else { 
    window.location.href = "geobytes.aspx"; 
}