仅继承母版页中的 javascript 代码

发布于 2024-10-19 15:01:34 字数 597 浏览 2 评论 0原文

所以我的母版页上有一个 Javascript 函数。当我从从此母版页派生的页面调用它时,它工作正常,但是,在不派生的页面上,它显然不起作用。我无法将其放入另一个 .js 文件中,因为它使用了一些嵌入的 C# 代码,因此它必须位于

这就是函数:

function GridExport(viewUrl, fileName)
    {
        var actionUrl = "<%= System.Web.VirtualPathUtility.ToAbsolute("~/mvc/Indications.cfc/ExportToExcel")%>";
        var guid = GetGUIDValue();
        window.open((actionUrl + "?id=" + guid + "&viewName=" + viewUrl + "&fileName="  + fileName), null, null, null);
    }

有没有一种方法可以将我的母版页中的 Javascript 定义包含在另一个视图中,而不从母版页派生?

So I have a Javascript function on my master page. It works fine when I call it from pages that derive from this master page, however, on pages that don't, it obviously doesn't work. I am not able to put it in another .js file either because it uses some embedded c# code in it, so it has to be within a <script> tag.

This is the function:

function GridExport(viewUrl, fileName)
    {
        var actionUrl = "<%= System.Web.VirtualPathUtility.ToAbsolute("~/mvc/Indications.cfc/ExportToExcel")%>";
        var guid = GetGUIDValue();
        window.open((actionUrl + "?id=" + guid + "&viewName=" + viewUrl + "&fileName="  + fileName), null, null, null);
    }

is there a way I can just include the Javascript definitions from my master page in another view without deriving from the master page?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沙沙粒小 2024-10-26 15:01:34

您可以创建一个包含此

function GridExport(viewUrl, fileName, actionUrl)
{
    var guid = GetGUIDValue();
    window.open((actionUrl + "?id=" + guid + "&viewName=" + viewUrl + "&fileName="  + fileName), null, null, null);
}

避免此路径问题的另一个简便方法是使用一个全局变量来定义站点的根:

var root = "<%= System.Web.VirtualPathUtility.ToAbsolute("~/") %>";

然后您的函数可以始终使用此变量并附加它需要的内容:

function GridExport(viewUrl, fileName)
{
    var actionUrl = root + "mvc/Indications.cfc/ExportToExcel";
    var guid = GetGUIDValue();
    window.open((actionUrl + "?id=" + guid + "&viewName=" + viewUrl + "&fileName="  + fileName), null, null, null);
}

You could create a UserControl containing this <script> or abstract out the actionUrl to a parameter of the GridExport function

function GridExport(viewUrl, fileName, actionUrl)
{
    var guid = GetGUIDValue();
    window.open((actionUrl + "?id=" + guid + "&viewName=" + viewUrl + "&fileName="  + fileName), null, null, null);
}

Another handy way to avoid this path problem would be to have a global variable to define the root of the site:

var root = "<%= System.Web.VirtualPathUtility.ToAbsolute("~/") %>";

then your function could always use this variable and append what it needs:

function GridExport(viewUrl, fileName)
{
    var actionUrl = root + "mvc/Indications.cfc/ExportToExcel";
    var guid = GetGUIDValue();
    window.open((actionUrl + "?id=" + guid + "&viewName=" + viewUrl + "&fileName="  + fileName), null, null, null);
}
一城柳絮吹成雪 2024-10-26 15:01:34

母版页可以相互嵌入。为什么不直接制作一个只包含 javascript 的母版页呢?然后制作另一个以 javascript 母版页作为母版页的母版页。创建新页面时,您可以选择使用哪一个页面。

MasterPage1 ->只是 javascript

MasterPage2 ->包含您的其他内容,并以 MasterPage1 作为其主控。

如果您只需要 javascript,则用户使用 MasterPage1,对于其他所有内容,用户使用 MasterPage2。

Master pages can be embedded within each other. Why not just make a master page with only your javascript in it. Then make another master page that has the javascript master page as it's master. When creating new pages you can choose which one to use.

MasterPage1 -> Just javascript

MasterPage2 -> Contains your other stuff and has MasterPage1 as it's master

If you just want the javascript then user MasterPage1, for everything else user MasterPage2.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文