简体   繁体   English

如何在 cshtml 文件中调用外部 javascript 文件

[英]How do i call external javascript file in cshtml file

Below is my Javascript file.下面是我的 Javascript 文件。

Copy.js复制.js

function CopyYourSheet() {

    var ifrm = document.createElement("iframe");
    ifrm.setAttribute("src", "https://docs.google.com/spreadsheets/d/1992ry0dpIjel_TQuB-W6fb2Nd7uST69AX_jY/edit#");
    ifrm.style.width = "10000px";
    ifrm.style.height = "2000px";
    document.body.appendChild(ifrm);
    }

Below is the CSHTML File下面是 CSHTML 文件

Index.cshtml索引.cshtml

@{
    ViewData["Title"] = "Home Page";
}

    <div class="text-center">
        <h1 class="display-4">Welcome</h1>                    
        <button id="CopyYourSheet" onclick="CopyYourSheet()">Get your details</button>
    </div>

The right way to include it is through bundling the Copy.js into your bundle.包含它的正确方法是将 Copy.js 捆绑到您的包中。

under App_Start folder open BundleConfig在 App_Start 文件夹下打开 BundleConfig

Then add然后加

bundles.Add(new ScriptBundle("~/bundles/myjs").Include(
                        "~/Scripts/copy.js"));

then include it in your Home view like this然后像这样将它包含在您的主视图中

@Scripts.Render("~/bundles/myjs")

If you do not have an App_Start folder or you do not prefer to bundle in an MVC project, then include the script inside of the <head> tags of your _Layout partial view.如果您没有 App_Start 文件夹,或者您不喜欢捆绑到 MVC 项目中,则将脚本包含在 _Layout 局部视图的<head>标记内。 Adding it right above the closing </head> tag would be best.最好将它添加到结束</head>标记的正上方。

<script src="~/Scripts/copy.js"></script>

Be sure the path to the file is correct.确保文件的路径正确。 You may need to remove the /Scripts/ folder in the src path, if the js file resides in your main project tree.如果 js 文件位于主项目树中,您可能需要删除 src 路径中的 /Scripts/ 文件夹。 The ~ should remain at the start of the path to make it flexible when the code is ran from different environments. ~ 应该保留在路径的开头,以便在从不同环境运行代码时使其灵活。

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

相关问题 如何从javascript / jquery调用cshtml文件? - How should I call a cshtml file from javascript/jquery? 如何将变量从外部JavaScript文件传递到CSHTML页面? - How to pass a variable from external javascript file to a cshtml page? 如何从一个cshtml文件调用或使用cshtml文件的功能? - How to call or use function of cshtml file from one cshtml file? 如何从外部 JavaScript 文件调用 Vue.js function? - How do I call a Vue.js function from an external JavaScript file? 如何将 cshtml 文件中的布尔值传递给 javascript 函数/构造函数? - How do I pass a boolean from a cshtml file into a javascript function/constructor? 我如何从另一个外部javascript文件中的外部javascript文件调用函数? - How i can call a function from external javascript file in another external javascript file? 如何将carousel()外部javascript函数调用为html文件? - how can i call carousel() an external javascript function into html file? 如何将Ajax调用包含为外部文件? - How do I include an Ajax call as an external file? 如何使用 typecrpt 在外部 js 文件中调用 function - How do I call a function in an external js file using typescrpt 代码在CSHTML中有效,但在外部Javascript文件中无效 - Code works in CSHTML but does not work in external Javascript file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM