9

是否可以将变量传递给链接的 .js 文件?我试过这个:

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

但是萤火虫告诉我没有定义超时。这是该 .js 文件的代码:

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

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

更新:

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

<%@ 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

4 回答 4

16

不,您不能传递这样的参数并让脚本读入它们。

从技术上讲,您可以从<script>标签中获取它们,但这将是一团糟。

您可以在包含文件之前输出一个脚本块吗?

<script type="text/javascript"> var timeout = 1000; </script>
于 2009-08-27T21:26:10.763 回答
14

尝试这个:

<script>
var myvariable = "foo";
</script>
<script src="/link/to/js.js"></script>
于 2009-08-27T21:26:16.553 回答
3
<script type="text/javascript">
var imagesPath = "emblematiq/img/";
</script>
<script type="text/javascript" src="emblematiq/niceforms.js"></script>

这将在服务器上正常工作

于 2012-10-07T08:50:41.687 回答
0

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

于 2009-08-27T21:26:11.997 回答