Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
仅从母版页继承javascript代码_Javascript_Asp.net Mvc 2_Master Pages - Fatal编程技术网

仅从母版页继承javascript代码

仅从母版页继承javascript代码,javascript,asp.net-mvc-2,master-pages,Javascript,Asp.net Mvc 2,Master Pages,所以我的母版页上有一个Javascript函数。当我从这个母版页派生的页面调用它时,它工作得很好,然而,在不工作的页面上,它显然不工作。我也不能把它放在另一个.js文件中,因为它使用了一些嵌入的c代码,所以它必须在标记中 这就是功能: function GridExport(viewUrl, fileName) { var actionUrl = "<%= System.Web.VirtualPathUtility.ToAbsolute("~/mvc/Indicat

所以我的母版页上有一个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);
    }
函数GridExport(viewUrl,文件名)
{
var actionUrl=“”;
var guid=GetGUIDValue();
window.open((actionUrl+“?id=“+guid+”&viewName=“+viewUrl+”&fileName=“+fileName”),null,null,null);
}

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

您可以创建一个包含此
用户控件
,或者将
actionUrl
抽象为
GridExport
函数的一个参数

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("~/") %>";

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

MasterPage1->只是javascript

MasterPage2->包含您的其他内容,并将MasterPage1作为其母版

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

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);
}