如何使用asp.net外部JavaScript文件 [英] How to use an external javascript file in asp.net

查看:89
本文介绍了如何使用asp.net外部JavaScript文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个脚本来隐藏或显示我的asp.net web应用程序加载器。内嵌放置在脚本的伟大工程。我试图脚本提取到外部文件并收到以下错误:

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:

错误:'暂停'属性的值为null或undefined,而不是一个函数对象

Error: The value of the property 'Pausing' is null or undefined, not a Function object

我试图查找错误,但我无法找到一个解决问题的办法。我是新来asp.net所以它可能是我不知道如何寻找正确的问题。

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.

我的直列code,它的工作原理是:

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>

本次活动的呼叫连接到一个asp:下面的按钮控件:

The event call is attached to an asp:button control below:

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

然后我收到previously提到的错误。

Then I receive the previously mentioned error.

任何帮助将大大AP preciated!

Any help would be greatly appreciated!

推荐答案

始终使用RESOLVEURL打电话给你的脚本文件,这样

Always use ResolveUrl to call your script files like this

让我们假设你的脚本是在一个文件名作为MyScriptFile.js

Lets assume your script is in Script folder of your root path with a file Name as MyScriptFile.js

 <script type="text/javascript" src="<%= ResolveUrl ("~/Scripts/MyScriptFile.js") %>"></script>  

编辑:您可以根据您的需要使用RESOLVEURL或ResolveClientUrl

EDIT : you can use ResolveUrl or ResolveClientUrl based on your needs

RESOLVEURL创建相对于根,其中作为URL
ResolveClientUrl创建相对于当前页面的URL。

ResolveUrl creates the URL relative to the root where as ResolveClientUrl creates the URL relative to the current page.

这篇关于如何使用asp.net外部JavaScript文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆