![](/img/trans.png)
[英]How do I add a javascript file from a UserControl to an ASP.NET page only once?
[英]How do I determine between a page calling a dynamic javascript file and a user in ASP.net
我正在使用在asp.net中生成的動態JavaScript文件,然后將其作為普通javascript嵌入到html頁面中。 我是否可以確定動態javascript是直接打開還是通過html頁面中的嵌入式腳本打開?
<script src="http://localhost:60919/js.ashx" type="text/javascript" charset="utf-8"></script>
如果是從網頁調用的,則應該看到Referer屬性中列出的加載頁面。
context.Request.UrlReferrer
要么
context.Request.ServerVariables["HTTP_REFERER"]
如果由用戶直接加載, 則應為null。 但是,垃圾郵件發送者/黑客可以手動設置引薦來源,以獨立於您的頁面訪問腳本。
更新:Handler.ashx:
<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Web;
public class Handler : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/javascript";
context.Response.Write("alert('"+context.Request.UrlReferrer+"');\n");
}
public bool IsReusable {
get {
return false;
}
}
}
Default.aspx:
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<script src="Handler.ashx"></script>
<h2>
Welcome to ASP.NET!
</h2>
<p>
To learn more about ASP.NET visit <a href="http://www.asp.net" title="ASP.NET Website">www.asp.net</a>.
</p>
<p>
You can also find <a href="http://go.microsoft.com/fwlink/?LinkID=152368&clcid=0x409"
title="MSDN ASP.NET Docs">documentation on ASP.NET at MSDN</a>.
</p>
</asp:Content>
網頁加載時,警報顯示http:// localhost:port / Default.aspx 。 直接訪問時,alert('')。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.