繁体   English   中英

将变量传递给外部JS文件?

[英]Pass variable to external JS file?

是否可以将变量传递到链接的.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));
    }
}

这样就达到了我的期望,也许这种方法可以帮助其他人。

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

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

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

<script type="text/javascript"> var timeout = 1000; </script>

尝试这个:

<script>
var myvariable = "foo";
</script>
<script src="/link/to/js.js"></script>
<script type="text/javascript">
var imagesPath = "emblematiq/img/";
</script>
<script type="text/javascript" src="emblematiq/niceforms.js"></script>

这将在服务器上正常工作

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM