我想根據是否啓用javascript來在部分視圖中呈現特定的HTML。我的想法是如何檢測javascript是否已啓用並將其存儲在會話變量中,然後我可以在我的視圖中測試它,例如,ASP.NET MVC2 - 檢測JavaScript,將結果存儲在會話變量中
<div class="product">
<%
// if Javascript is enabled, load the images asynchrously, otherwise load them directly.
string imgHtml = "<img id=\"myImage\" src=\"{0}\" alt=\"MyImage\" />";
if ((bool)Session["jsEnabled"])
imgHtml = String.Format(imgHtml, Url.Content("~/Content/images/ajax-loader.gif") + "\" onload=\"loadImageAsync(this, '" + Url.Content("~/Content/images/" + Model.thumbnail) + "')");
else
imgHtml = String.Format(imgHtml, Url.Content("~/Content/images/image.jpg"));
%>
<div><%= imgHtml %></div>
</div>
從我讀書最多的,似乎可以用一個隱藏字段,並設置它在JavaScript值建議,但是,我不太明白,我怎麼能得到這個在MVC工作,除非我的地方發佈數據(這個特定的視圖不在表單中)。
我想要做的是讓圖像加載異步,如果啓用JavaScript,否則加載圖像的頁面。加載的正常圖像是非常大的jpeg(這種局部視圖是爲每個圖像呈現圖像),因此確實要加載基本HTML例如src =「image.jpg」,是否會取消任何性能提升? – James 2010-08-18 23:44:01