[英]Function works when I put it in an onclick but doesn't work when I try to use in on page-load (JavaScript)
I am building a Microsoft VSTS extension using HTML
JavaScript
.我正在使用HTML
JavaScript
构建 Microsoft VSTS 扩展。
I need to access this function VSS.getWebContext()
which gives you all information of the project page where you are currently running your extension.我需要访问此函数VSS.getWebContext()
,它为您提供当前运行扩展的项目页面的所有信息。
let webContext = VSS.getWebContext();
let projectName = webContext.project.name + "_kpi_test"
# prints out fine
console.log('projname', projectName);
When I put this into an onclick
function of a button
, it works fine and prints the information that I need.当我把它放到一个button
的onclick
函数中时,它工作正常并打印我需要的信息。
But when I just use但是当我刚使用
$(window).ready(function(){
// your code
});
and和
$(document).ready(function(){
// your code
});
and和
window.onload = codeAddress; //my function here
to use the VSS.getWebContext()
to retrieve the information when the page loads, it keeps giving me that VSS.getWebContext()
is undefined
.要在页面加载时使用VSS.getWebContext()
检索信息,它一直给我VSS.getWebContext()
是undefined
。
If it works on onClick but not on page-load, what could be the problem?如果它适用于 onClick 但不适用于页面加载,可能是什么问题?
I am not sure what exactly VSS needs, but since it is working in onClick it is probably just that the page has not actually completely loaded yet in your document and window ready calls. 我不确定VSS到底需要什么,但是由于VSS在onClick中工作,可能只是页面尚未真正加载到文档和窗口就绪调用中。
You could try: 您可以尝试:
$(window).bind("load", function() {
// code here
});
There is an VSS.init
and VSS.ready
function, see use the SDK from vss-web-extension-sdk GitHub for more information.有一个VSS.init
和VSS.ready
函数,请参阅使用来自vss-web-extension-sdk GitHub的 SDK了解更多信息。
// VARIABLES VSS
extensionInitializationOptions: IExtensionInitializationOptions = { applyTheme: false, usePlatformScripts: false, usePlatformStyles: false };
async ngOnInit(): Promise<void> {
if (environment.production) {
// VSS INIT
VSS.init(this.extensionInitializationOptions);
// VSS READY
VSS.ready(async () => {
await this.init();
});
} else await this.init();
}
environment.production
for local debugging.用于本地调试的environment.production
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.