动态加载Js——ashx的妙用
前台代码:
View Code
1 <script type="text/javascript" src="Code/ProcessHandler.ashx?type=ad">
2 </script>
2 </script>
添加ProcessHandler.ashx
View Code
1 using System;
2 using System.Collections;
3 using System.Data;
4 using System.Web;
5 using System.Web.Services;
6 using System.Web.Services.Protocols;
7
8 namespace WebSite.Code
9 {
10 /// <summary>
11 /// $codebehindclassname$ 的摘要说明
12 /// </summary>
13 [WebService(Namespace = "http://tempuri.org/")]
14 [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
15 public class ProcessHandler : IHttpHandler
16 {
17
18 public void ProcessRequest(HttpContext context)
19 {
20 context.Response.ContentType = "text/plain";
21 string type = context.Request.Params["type"];
22 switch (type)
23 {
24 case "ad":
25 {
26 var focus_width=951;
27 var focus_height=200;
28 var text_height=0;
29 var swf_height = focus_height+text_height;
30
31 var pics="../flash/01.jpg|../flash/02.jpg|../flash/03.jpg";
32 var links="";
33 var texts="";
34
35 context.Response.Write("document.write('<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" style=\"border:none;\" codebase=\"http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0\" width=\""+ focus_width +"\" height=\""+ swf_height +"\">');\n");
36 context.Response.Write("document.write('<param name=\"allowScriptaccess\" value=\"sameDomain\"><param name=\"movie\" value=\"../Images/conove.swf\"><param name=\"quality\" value=\"high\"><param name=\"bgcolor\" value=\"#F0F0F0\">');\n");
37 context.Response.Write("document.write('<param name=\"menu\" value=\"false\"><param name=wmode value=\"opaque\">');\n");
38 context.Response.Write("document.write('<param name=\"FlashVars\" value=\"pics="+pics+"&links="+links+"&texts="+texts+"&borderwidth="+focus_width+"&borderheight="+focus_height+"&textheight="+text_height+"\">');\n");
39 context.Response.Write("document.write('<embed src=\"../Images/conove.swf\" wmode=\"opaque\" FlashVars=\"pics="+pics+"&links="+links+"&texts="+texts+"&borderwidth="+focus_width+"&borderheight="+focus_height+"&textheight="+text_height+"\" menu=\"false\" bgcolor=\"#F0F0F0\" quality=\"high\" width=\""+ focus_width +"\" height=\""+ swf_height +"\" allowScriptaccess=\"sameDomain\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" />');\n");
40 context.Response.Write("document.write('</object>');");
41
42 break;
43 }
44 }
45
46 }
47
48 public bool IsReusable
49 {
50 get
51 {
52 return false;
53 }
54 }
55 }
56
2 using System.Collections;
3 using System.Data;
4 using System.Web;
5 using System.Web.Services;
6 using System.Web.Services.Protocols;
7
8 namespace WebSite.Code
9 {
10 /// <summary>
11 /// $codebehindclassname$ 的摘要说明
12 /// </summary>
13 [WebService(Namespace = "http://tempuri.org/")]
14 [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
15 public class ProcessHandler : IHttpHandler
16 {
17
18 public void ProcessRequest(HttpContext context)
19 {
20 context.Response.ContentType = "text/plain";
21 string type = context.Request.Params["type"];
22 switch (type)
23 {
24 case "ad":
25 {
26 var focus_width=951;
27 var focus_height=200;
28 var text_height=0;
29 var swf_height = focus_height+text_height;
30
31 var pics="../flash/01.jpg|../flash/02.jpg|../flash/03.jpg";
32 var links="";
33 var texts="";
34
35 context.Response.Write("document.write('<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" style=\"border:none;\" codebase=\"http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0\" width=\""+ focus_width +"\" height=\""+ swf_height +"\">');\n");
36 context.Response.Write("document.write('<param name=\"allowScriptaccess\" value=\"sameDomain\"><param name=\"movie\" value=\"../Images/conove.swf\"><param name=\"quality\" value=\"high\"><param name=\"bgcolor\" value=\"#F0F0F0\">');\n");
37 context.Response.Write("document.write('<param name=\"menu\" value=\"false\"><param name=wmode value=\"opaque\">');\n");
38 context.Response.Write("document.write('<param name=\"FlashVars\" value=\"pics="+pics+"&links="+links+"&texts="+texts+"&borderwidth="+focus_width+"&borderheight="+focus_height+"&textheight="+text_height+"\">');\n");
39 context.Response.Write("document.write('<embed src=\"../Images/conove.swf\" wmode=\"opaque\" FlashVars=\"pics="+pics+"&links="+links+"&texts="+texts+"&borderwidth="+focus_width+"&borderheight="+focus_height+"&textheight="+text_height+"\" menu=\"false\" bgcolor=\"#F0F0F0\" quality=\"high\" width=\""+ focus_width +"\" height=\""+ swf_height +"\" allowScriptaccess=\"sameDomain\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" />');\n");
40 context.Response.Write("document.write('</object>');");
41
42 break;
43 }
44 }
45
46 }
47
48 public bool IsReusable
49 {
50 get
51 {
52 return false;
53 }
54 }
55 }
56