2015-11-25 32 views
0

我有不同屏幕尺寸3個不同的index.html型的網頁:index.html的爲> 1280,indexsm.html爲< = 1024和indexmed.html爲< = 1280document.location再直接循環

我以爲我能解決這個問題有以下JS:

if (screen.width <= 1024) 
{ 
    document.location = "indexsm.html" 
} 
else if (screen.width <= 1280) 
{ 
    document.location = "indexmed.html" 
} 
else 
{ 
    document.location = "index.html" 
} 

打開index.html的屏幕1024或更低時,重定向完美的作品,並重定向到indexsm.html。 在1280的屏幕上打開index.html,重定向工作完美並打開indexmed.html。 在1920年的屏幕上打開index.html(例如),我的jacascript循環並不斷重新加載index.html頁面。

有沒有人有解決方案?我搜索了幾個小時,一直無法找到治療方法。

回答

1

假設這個腳本是index.html,爲什麼你會重新設置位置(並因此重新加載頁面)呢?

if (screen.width <= 1024) 
{ 
    document.location = "indexsm.html" 
} 
else if (screen.width <= 1280) 
{ 
    document.location = "indexmed.html" 
} 

// else we're already where we need to be, so stop 
+1

這就是說,這是*不是*推薦或喜愛的方法,因爲大量的原因。請求,請求,查看[Responsive Design](https://responsivedesign.is/)。 –

+0

謝謝保羅。這很好。回答你爲什麼重新設置位置的問題:我想只是因爲我不擅長這一點。 RE:響應式設計,謝謝你的信息。我只是這樣做是爲了免費幫助朋友,不想了解響應式設計和柔性電網。如果我的朋友對這種方式如此傾向或存在問題,他可以自由支付他選擇的設計師。如果是這樣的話,我會傳遞你的信息;)....再次感謝。乾杯。 – GregF

+0

...但我可以肯定地看到你來自哪裏; IE和Safari似乎不喜歡這個代碼。也許迴應是值得一看的。再次感謝Paul。 – GregF