动态包含基于屏幕大小不起作用的文件

时间:2016-03-30 16:40:32

标签: javascript cookies vbscript asp-classic

我有一个测试页面,其语法在桌面上工作正常,但似乎不适用于移动设备并返回500内部服务器错误。关于如何纠正这个问题的任何想法?

<html>
<head>
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
var xy = navigator.appVersion;
xz = xy.substring(0,4);
document.cookie = "ScreenWidth=" + screen.width
document.cookie = "ScreenHeight=" + screen.height
// End -->
</script>
</head>
<body>
<%
'Declare variables
Dim strScreenWidth
Dim strScreenHeight
'Read the cookies with the width and height
strScreenWidth = Request.Cookies("ScreenWidth")
strScreenHeight = Request.Cookies("ScreenHeight")
%>
<% If strScreenWidth > 748 Then %>
  <!--#include file="alpha-inc1.asp"-->
<% Else %>
  <!--#include file="alpha-inc2.asp"-->
<% End If %>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

tl; dr:它不起作用。早在任何ASP代码运行之前,IIS都会处理#include指令。

更长的答案:无论您在<%if...then%>指令中添加了多少#include个语句,最终结果都将包含所有文件,因为包括由IIS处理,而不是由ASP处理,并且在任何代码解析之前很久就会发生。如果您希望代码选择是否运行文件A与文件B中的代码,则需要编写自己的伪包含进程。

最简单的是忘记条件包含,而是将代码放入可以有条件地调用的子例程中。

如果您绝对必须将代码分离为单独的文件,那么您可以将相应文件的内容作为一个长字符串读取,然后可以Execute。这要求该文件包含100%可执行的ASP代码,没有<% %>标签或html。见How to implement conditional includes in ASP using VBScript.