简体   繁体   English

从外部Javascript文件调用方法

[英]Calling a method from an external Javascript file

I have a external JS function defined below held within test.js 我在test.js中保存了下面定义的外部JS函数

function InvokeSupport(ID, TimeStamp, Hash) {

    var sUrl = '<%= System.Configuration.ConfigurationManager.AppSettings["URL"] %>' + "?uid=" + ID + "&t=" + TimeStamp + "&hash=" + Hash;

    window.open(sUrl, "Support", null, false);   
}

On my asp.net page I have the following. 在我的asp.net页面上,我有以下内容。

<script type="text/javascript" src="../../../scripts/test.js"></script>

<div class="User">
    <span class="UserName"><button type="submit" title="Help!" onclick="InvokeSupport()" class="Class1"></button> </span>
</div>

THE PROBLEM 问题

The InvokeSupport function is coming back as undefined and fails to work unless I place the function on the ASP.net page where it works. InvokeSupport函数以未定义的形式返回,并且无法工作,除非我将该函数放在ASP.net页面上。 The link to the javascript file is right as I have other files in the same repository working fine. javascript文件的链接是正确的,因为我在同一个存储库中的其他文件正常工作。

Any idea what Im doing wrong? 知道我做错了吗?

Why are you using ASP.NET script tags in external js file? 为什么在外部js文件中使用ASP.NET脚本标记?

Pass the data from configuration as and additional parameter to javascript function. 将配置中的数据和附加参数传递给javascript函数。

Also, you can use Firebug and Chrome devTools which can show you a place of error occured 此外,您可以使用Firebug和Chrome devTools,它可以显示错误发生的位置

This should work. 这应该工作。

function InvokeSupport(url, ID, TimeStamp, Hash) {

    var sUrl = url + "?uid=" + ID + "&t=" + TimeStamp + "&hash=" + Hash;

    window.open(sUrl, "Support", null, false);   
}


<script type="text/javascript" src="../../../scripts/test.js"></script>

<div class="User">
    <span class="UserName"><button type="submit" title="Help!" onclick="InvokeSupport('<%= System.Configuration.ConfigurationManager.AppSettings["URL"] %>',1,2,3)" class="Class1"></button> </span>
</div>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM