if page is default then include if not default then

橙三吉。 提交于 2019-12-12 04:05:58

问题


I've read similar questions, but none seems working for me.

I have an asp site, i think classic (don't even know difference between classic and net), with few pages all with asp extension, and some html includes, one of these being a slider which I want to only display when in homepage (default.asp), and if not default then add a div to the markup.

I know the following is wrong, but just to better explain my need.

<%
if page == default.asp
include file="slider.html"
if page != default.asp
<div class="spacer-top"></div>
%>

回答1:


Like many of your predecessors in ASP-classic-land, what you're wanting is conditional includes, and the problem you're bumping into is that classic ASP doesn't do conditional includes. (The reason why is that the #include directive is handled long before any script on the page is parsed.)

There are various workarounds involving Execute or other dangerous-in-the-wrong-hands commands; search for "asp conditional include" and you'll find more than you were bargaining for. However, in your case, it might be simpler to encase the slider display in a subroutine that you can call or not.

Slider.html:

<%
Sub DisplaySlider()
    'code to display the slider (probably JavaScript, I'm guessing?)
    %>
    <script ...>
    </script>
    <%
End Sub
%>

Other pages:

<!-- #include virtual="/slider.html" -->
<%
scriptname = Request.ServerVariables("Script_Name")
If InStr(scriptname, "default.asp") > 0 Then
    DisplaySlider
Else
    Response.Write "<div class='spacer-top'></div>"
End If
%>



回答2:


Martha is bang on the nail.

To augment her answer I feel I should point out that system design comes into play, here. Try keeping your modules small and succinct, targeting their functionality to a particular aspect of your application's requirements. For example:

  • one to deal with your data layer
  • one to deal with more advanced form handling
  • one to deal with blah

You get the idea.

Another idea is to include common functions in your global.asa so they're available to all modules within your application instantly.



来源:https://stackoverflow.com/questions/35610851/if-page-is-default-then-include-if-not-default-then

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!