简体   繁体   English

当我将其放入 onclick 时该函数有效,但当我尝试在页面加载 (JavaScript) 中使用时该函数不起作用

[英]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.当我把它放到一个buttononclick函数中时,它工作正常并打印我需要的信息。

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.initVSS.ready函数,请参阅使用来自vss-web-extension-sdk GitHub的 SDK了解更多信息。

In Angular & TypeScript:在 Angular 和 TypeScript 中:

  // 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.

相关问题 当我将它与 onClick 中的其他 function 一起使用时,useState 不起作用 - useState doesn't work when I use it with other function in onClick 当我尝试暂停并在jsFiddle中播放时,我的代码可以工作,但在我的html页面中不起作用 - When I try pause and play in jsFiddle, my code works but it doesn't work in my html page 将按钮<a onclick>发送到另一个页面时</a> ,只有按钮onclick起作用, <a onclick>标记在javascript中不起作用</a> - Only button onclick works, <a onclick> tag doesn't work in javascript when sending parameter to another page 当我使用onsubmit而不是onclick时,JavaScript代码不起作用 - Javascript code doesn't work when I use onsubmit instead of onclick 放入 JavaScript 脚本时 onclick 不起作用 - onclick doesn't work when put in JavaScript script 初学者/我尝试使用 Onclick 取消隐藏元素,但它不起作用 - Beginner/ I try to use Onclick to unhide element but it doesn't work 当我尝试在页面加载时间或通过onclick函数调用它时,Magnific Popup无法正常工作 - Magnific Popup Not working properly when i try to call it on page load time or via onclick function $(function(){})中定义的函数时,Ajax页面onclick不起作用 - Ajax page onclick doesn't work when function defined in $(function(){ }) 加载html页面时,为什么我的外部javascript文件不起作用? - Why doesn't my external javascript file work when I load my html page? 当我使用模板时,Javascript函数不起作用 - Javascript function dosn't works when I work with templates
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM