2015-04-19 85 views
0
<body style="margin:0px;padding:0px;overflow:hidden"  onload="img.src='http://sjabbo.net/logo.png?'+(+new Date())"> 
<img id="img" 
    onload="iframe.src='http://sjabbo.net'" 
    onerror="iframe.src='offline.html'" 
width="0px" height="0px"> 

<iframe width="100%" height="1000px"></iframe> 
</body> 

所以,如果本地用戶擁有互聯網連接,然後將IFRAME應更改爲「http://sjabbo.net」,但如果沒有,那麼「offline.html」 但它似乎不工作。的Onload IFRAME不會工作

+0

爲什麼你不要在onload上調用函數,然後在那個函數中分配img? – abaracedo

+0

你能爲此發佈一個代碼嗎? –

回答

0

如何:

<body style="margin:0px;padding:0px;overflow:hidden"> 
<img id="img" src="http://sjabbo.net/logo.png" 
    onload="document.getElementById('iframe').src='http://sjabbo.net'"/> 

<iframe src="offline.html" id="iframe" width="100%" height="1000px"></iframe> 
</body> 

你可以推出 「測試」 頁面是這樣的:

<body style="margin:0px;padding:0px;overflow:hidden"> 
<img id="img" src="http://sjabbo.net/logo.png" 
    onload="document.getElementById('iframe').src='http://sjabbo.net'" 
    onerror="document.getElementById('iframe').src='offline.html'"/> 

<iframe src="testing.html" id="iframe" width="100%" height="1000px"></iframe> 
</body> 
0

首先,你不能只使用「img」,因爲對於JavaScript而言,它並沒有在全球任何地方定義。如果您想通過其ID訪問圖像,則需要執行以下操作。此外,我猜你想要有一個時間戳,而不是長日期。把這樣的事情在你的onload

document.getElementById('img').src = 'http://sjabbo.net/logo.png?' + (new Date()).getTime() 

這樣做與您的IFRAME(你必須先爲其分配一個ID)。

0

我不認爲你已經明白網絡是如何工作的。

您不需要爲每個有孩子的元素添加onload。每個元素都是一個接一個地創建的。所以現在刪除所有的onloads。

您似乎想等待以瞭解您的圖像是否正確加載。根據這個你想激活你的iframe。

你會想要創建一個JavaScript標籤(或者爲了好的做法,創建一個js文件)。

var img = document.getElementById('img'), 
    iframe = document.getElementById('iframe'); //add iframe as an id to your iframe 
img.onload = function(){ 
    //this is called when the image is finished loading. 
    iframe.src = 'http://sjabbo.net'; 
}; 
img.onerror = function(){ 
    //this is called when the image wasn't able to load properly 
    iframe.src = 'offline.html'; //To be honest this doesn't make much sense 
    //You should instead just hide the iframe and the image 
    iframe.style.visibility = 'none'; 
    img.style.visibility = 'none'; 
}; 
img.src = 'http://sjabbo.net/logo.png?' + (new Date()).getTime(); 

對於將來的編程,請嘗試從您的html分離您的js和css。你現在在做什麼不是很好的做法。