代码之家  ›  专栏  ›  技术社区  ›  Anders

是否将变量传递到外部JS文件?

  •  7
  • Anders  · 技术社区  · 15 年前

    <sf:JsFileLink ID="JQueryLoader" runat="server" ScriptType="Custom" FileName="~/Files/Scripts/rotatorLoader.js?timeout=1000" />
    

    但是firebug告诉我超时时间没有定义。下面是该.js文件的代码:

    $(document).ready(function() {
        $("#rotator > ul").tabs({ fx: { opacity: "toggle"} }).tabs("rotate", timeout, true);
    });
    

    我正在使用 <sf:JsFileLink ... /> 标记是因为我所在的网站利用sitefinity,该标记允许我加载外部.js文件。

    更新:

    我能够通过创建一个模拟javascript页面的aspx页面来“欺骗”include:

    <%@ Page Language="C#" %>
    
    <%
        Response.ContentType = "text/javascript";
        Response.Clear();
        string timeout;
        try
        {
            timeout = Session["timeout"].ToString();
        }
        catch
        {
            timeout = "4000";
        }
    %>
    
    $(document).ready(function() {
        $("#rotator > ul").tabs({ fx: { opacity: "toggle"} }).tabs("rotate", <%=timeout %>, true);
    });
    

    和在用户控制页上:

    [DefaultProperty("BannerTimeout")]
    public partial class Custom_UserControls_TabbedRotator : System.Web.UI.UserControl
    {
        [Category("Configuration")]
        [Description("Sets the rotation timeout, in seconds.")]
        [DisplayName("Banner Timeout")]
        public int BannerTimeout { get; set; }
    
        protected void Page_Load(object sender, EventArgs e)
        {
            Session.Add("timeout", (BannerTimeout*1000));
        }
    }
    

    这实现了我所期待的,也许这个方法可以帮助其他人。

    4 回复  |  直到 15 年前
        1
  •  16
  •   Greg    15 年前

    不,您不能像那样传递参数并让脚本读取它们。

    从技术上讲,你可以从车上抓到它们 <script> 标签,但那将是一个真正的混乱。

    在包含该文件之前,是否可以输出一个脚本块?

    <script type="text/javascript"> var timeout = 1000; </script>
    
        2
  •  14
  •   Byron Whitlock    15 年前

    试试这个:

    <script>
    var myvariable = "foo";
    </script>
    <script src="/link/to/js.js"></script>
    
        3
  •  3
  •   Developer-Sid    12 年前
    <script type="text/javascript">
    var imagesPath = "emblematiq/img/";
    </script>
    <script type="text/javascript" src="emblematiq/niceforms.js"></script>
    

        4
  •  0
  •   Andrew Magill    15 年前

    不可以,但您可以将值直接传递给该文件中的函数,或设置将在外部文件中使用的变量值。