[英]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.