簡體   English   中英

根據用戶屏幕大小加載js或php

[英]load js or php based on users screen size

我有幾個Swiffy(HTML5 SWF)動畫,我只想根據用戶的屏幕大小進行加載!

iv在下面找到了一些JavaScript,其中說如果screnn大於1400,它將加載,但不能正常工作!

<script language=javascript>
if(screen.Width>1400)
{
    document.write("<?php include('/map/map4.php');?>");
}
else
{
    document.write("<?php include('/map/map1.php');?>");
}
</script>

用上述方法或另一種方法可以使任何人知道這樣做嗎?

謝謝

更改此:

if(screen.Width>1400)

至:

if (screen.width > 1400)

screen對象的width屬性應為小寫。

還要注意,當執行JavaScript時,ServerSide處理結束,並且document.write僅輸出文本而不是可執行的php代碼,您可以改用jQuery的load方法。

從服務器加載數據,並將返回的HTML放入匹配的元素。

if (screen.width > 1400) {
     $('#wrapper').load('/map/map4.php');
} else {
     $('#wrapper').load('/map/map1.php');
}

這些php文件的內容將被注入JavaScript文字中,並可能破壞該文字。 從瀏覽器查看時,您可以發布結果html嗎?

可能您想做的是將兩個php文件放入單獨的div代碼中,然后使用CSS隱藏或顯示相關的文件。

如果執行此操作,則可以使用@media css查詢根據窗口大小顯示/隱藏各節,並且隨着用戶調整瀏覽器的大小,這將動態調整。

如果只有SWF對象,則可以通過DOM操作將它們添加到文檔中

如果您想以更復雜的方式檢查用戶屏幕,則有一個庫jRespond ,它將輪詢瀏覽器的大小並在用戶調整瀏覽器大小時進行更新。

我正面臨着同樣的困境,所以我使用了這種解決方法,希望對您有所幫助。

<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>435</title>
<script>  if( document.cookie.indexOf("resolucion5") < 0) {
//alert("Cargando resolucion.");


if (screen.width > 1199){ document.cookie = "resolucion5=desktop; max-age=" + 5; }
if ((screen.width > 768)&&(screen.width < 1200)){ document.cookie = "resolucion5=tablet; max-age=" + 5; }
if ((screen.width > 380)&&(screen.width < 780)){ document.cookie = "resolucion5=cel; max-age=" + 5; }
if (screen.width < 381){ document.cookie = "resolucion5=mini; max-age=" + 5; 
}

location.reload();
}</script>


<? if ($_COOKIE["resolucion5"]=="desktop"){$resolucion="";}
elseif ($_COOKIE["resolucion5"]=="tablet"){$resolucion="med";}
elseif ($_COOKIE["resolucion5"]=="cel"){$resolucion="med";}
elseif ($_COOKIE["resolucion5"]=="mini"){$resolucion="mini";} ?>


<link href="style<? echo $resolucion; ?>.php" rel="stylesheet">
 <script src="somescript<? echo $resolucion; ?>.js" type="text/javascript"></script>

</head>

<body>
<? require("menu".$resolucion.".php"); ?><br>
<img src="welcome<? echo $resolucion; ?>.jpg">
</body>
</html>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM