如何在 asp.net 中使用外部 javascript 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11297189/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use an external javascript file in asp.net
提问by jfielaidof
I wrote a script to hide and show a loader for my asp.net web application. The script works great when placed inline. I tried to extract the script to an external file and received the following error:
我编写了一个脚本来隐藏和显示我的 asp.net Web 应用程序的加载程序。该脚本内嵌时效果很好。我尝试将脚本提取到外部文件并收到以下错误:
Error:The value of the property 'Pausing' is null or undefined, not a Function object
错误:属性“Pausing”的值为 null 或未定义,不是 Function 对象
I tried to look up the error, but I was unable to find a solution to the problem. I am new to asp.net so it may be that I'm not sure how to search for the right question.
我试图查找错误,但无法找到问题的解决方案。我是 asp.net 的新手,所以我可能不确定如何搜索正确的问题。
My inline code that works is:
我有效的内联代码是:
<script type="text/javascript">
function Pausing() {
window.setTimeout(ShowLoader, 1);
}
function ShowLoader() {
if ((typeof Page_IsValid === 'undefined') ||
(Page_IsValid != null && Page_IsValid)) {
var i = document.getElementById("loader");
var img = document.getElementById("img");
i.style.display = "block";
setTimeout("document.images['img'].src=document.images['img'].src", 10);
Endpausing();
}
}
function HideLoader() {
var i = document.getElementById("loader");
i.style.display = "none";
}
function Endpausing() {
window.setTimeout(HideLoader, 4000);
}
</script>
The event call is attached to an asp:button control below:
事件调用附加到下面的 asp:button 控件:
<asp:Button ID="btnGetReport" runat="server" OnClick="btnGetReport_Click" OnClientClick="Pausing();" />
I removed the inline script and replaced with this...
我删除了内联脚本并替换为这个......
<script type="text/javascript" src="../../Scripts/Loader.js"></script>
Added script to external file:
将脚本添加到外部文件:
window.onload = initAll;
function initAll() {
function Pausing() {
window.setTimeout(ShowLoader, 1);
}
function ShowLoader() {
if ((typeof Page_IsValid === 'undefined') || // asp page has no validator
(Page_IsValid != null && Page_IsValid)) {
var i = document.getElementById("loader");
var img = document.getElementById("img");
i.style.display = "block";
setTimeout("document.images['img'].src=document.images['img'].src", 10);
Endpausing();
}
}
function HideLoader() {
var i = document.getElementById("loader");
i.style.display = "none";
}
function Endpausing() {
window.setTimeout(HideLoader, 4000);
}
}
Then I receive the previously mentioned error.
然后我收到前面提到的错误。
Any help would be greatly appreciated!
任何帮助将不胜感激!
回答by Suave Nti
Always use ResolveUrl to call your script files like this
始终使用 ResolveUrl 像这样调用您的脚本文件
Lets assume your script is in Script folder of your root path with a file Name as MyScriptFile.js
假设您的脚本位于根路径的 Script 文件夹中,文件名为 MyScriptFile.js
<script type="text/javascript" src="<%= ResolveUrl ("~/Scripts/MyScriptFile.js") %>"></script>
EDIT : you can use ResolveUrl or ResolveClientUrl based on your needs
编辑:您可以根据需要使用 ResolveUrl 或 ResolveClientUrl
ResolveUrl creates the URL relative to the root where as ResolveClientUrl creates the URL relative to the current page.
ResolveUrl 创建相对于根的 URL,而 ResolveClientUrl 创建相对于当前页面的 URL。
回答by HatSoft
Based on your question : How to use an external javascript file in asp.net
基于您的问题:How to use an external javascript file in asp.net
<script type="text/javascript" src="http://www.xyz.com/test.js"></script>