javascript 如何从aspx页面调用外部javascript函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11330356/
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 call a external javascript function from aspx page?
提问by Hulk
I have a javascriptfile file.js
and I have a function FunctionfromJSpage()
in that jspage, first from onclickI was calling the function aspxPageFunction();
then from this function I need to call FunctionfromJSpage();
which is located in file.js
.
I tried like this in default.aspx
我有一个javascript文件,file.js
并且FunctionfromJSpage()
在那个js页面中有一个函数,首先从onclick我调用该函数aspxPageFunction();
然后从这个函数我需要调用FunctionfromJSpage();
它位于file.js
.
我试过这样default.aspx
<head>
<script type="text/javascript" src="../page/file.js"></script>
<script type="text/javascript">
function aspxPageFunction() {
//my code..
FunctionfromJSpage();
}
</script>
</head>
But I am unable to call that function.
但我无法调用该函数。
回答by sacredfaith
harish,
粗暴的,
I think what you're looking for is to add the function you've now defined (as Quentin pointed out) to an attribute of an existing asp control. For example:
我认为您正在寻找的是将您现在定义的函数(如 Quentin 指出的)添加到现有 asp 控件的属性中。例如:
<td>
<asp:Button ID="Button2" runat="server" OnClientClick="YourFunction(); " Text="ClickMe"/>
</td>
You can do all sorts of things using this kind of methodology. It all depends on what you want to do. If you're trying to get something to happen when a user puts text in a textbox, you'll need to research the OnTextChanged event, dropdowns have the onchange event..perhaps a crash course in asp events might be useful to help you understand more about whereto attach such things.
你可以使用这种方法做各种各样的事情。这一切都取决于你想做什么。如果您试图在用户将文本放入文本框中时发生某些事情,您需要研究 OnTextChanged 事件,下拉列表具有 onchange 事件..也许 asp 事件中的速成课程可能有助于您理解更多关于在哪里附加这些东西。
For client side (javascript events).
对于客户端(javascript 事件)。
For server side - what we're talking about here - (asp events).
对于服务器端 - 我们在这里谈论的是 - (asp events)。
It looks like you're just starting to figure out the client server relationship and what you need to do to make things happen (I could be verywrong there though). If this is the case, I wish you well! I agree with the other posters - you should explore Google and this site a bit more, as these are VITAL resources to finding breakthrough on your coding endeavors.
它看起来像你刚刚起步弄清楚客户端服务器的关系,你需要做的,使事情发生(我可能是非常错误的存在虽然)。如果是这样,祝你一切顺利!我同意其他海报 - 你应该更多地探索谷歌和这个网站,因为这些是在你的编码工作中找到突破的重要资源。
-sf
-sf
回答by Quentin
The code you have in your script element definesa function.
脚本元素中的代码定义了一个函数。
You say that you have defined it in file.js. To call it you just callFunction()
.
你说你已经在file.js中定义了它。把它叫做你只是callFunction()
。
<script type="text/javascript" src="../page/file.js"></script>
<script type="text/javascript">
callFunction();
</script>
回答by Ashwin Singh
Add the onload attribute to your body, this will call your function when the page loads <body onload="callFunction()">
将 onload 属性添加到您的正文中,这将在页面加载时调用您的函数 <body onload="callFunction()">
or in codebehind
或在代码隐藏中
Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "scriptKey", "callFunction();",true);
Have a look at this great article http://www.codeproject.com/Articles/11098/Use-Call-RegisterStartUpScript-RegisterClientScrip
看看这篇很棒的文章http://www.codeproject.com/Articles/11098/Use-Call-RegisterStartUpScript-RegisterClientScrip