我有兩個頁面,一個是index.html
另一個是iframe.html
JS,如何讓頁面只能作爲iframe加載到另一個頁面?
如何讓iframe.html
在index.html
只能加載?
如果IFRAME在other pages
或直接鍵入瀏覽器http://www.site.com/iframe.html
,頁面僅顯示一些詞像opps~~something is wrong.
像bing shopping
。
謝謝。
我有兩個頁面,一個是index.html
另一個是iframe.html
JS,如何讓頁面只能作爲iframe加載到另一個頁面?
如何讓iframe.html
在index.html
只能加載?
如果IFRAME在other pages
或直接鍵入瀏覽器http://www.site.com/iframe.html
,頁面僅顯示一些詞像opps~~something is wrong.
像bing shopping
。
謝謝。
您可以使用window.parent.location
來獲取iframe中的父URL並相應地執行操作。如果未定義window.parent,則可能意味着直接調用了iframe.html。如果已定義,請檢查該值並確保它來自您的域上的/index.html。
我想這應該這樣做...
var inIframe = window.location != window.parent.location;
您可以在Iframe.html的使用此javascript:
if (top.location.href != 'http://www.site.com/index.html') {
// the proper redirect here
top.location.href = 'http://www.site.com/index.html';
}
這種方式,將永遠被載入爲I幀
Diallo,儘量使用你給的東西,但是當打開'index.html'時,實際上當你打開index.html的時候頁面總會刷新 – 2011-05-03 21:36:58
它不應該刷新,如果頁面不同,條件只是'true' 。此代碼進入iframe.html頁面而不是index.html – Ibu 2011-05-03 22:02:12
if(self == top)
location.href='/';
把這個在iframe頁面
@s imshaun,@Darin Dimitrov,'window.parent.location'只是將'iframe.html'重定向到'index.html',但我需要一些效果,比如'bing shopping' – 2011-05-03 21:48:41
@yuli chika,'window.parent.location' doesn不會重定向到任何地方:它會爲您提供父託管頁面的位置(如果有)。如果您修改該值,則會重定向。但在這裏我們正在討論閱讀它,並根據其價值作出相應的行動。順便說一句,這是'iframe.html'知道父容器的唯一方法。 – 2011-05-03 21:50:52