当我把它放在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)

查看:66
本文介绍了当我把它放在onclick中但是当我尝试在页面加载时使用时它不起作用(JavaScript)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 HTML JavaScript 构建Microsoft VSTS扩展。

I am building a Microsoft VSTS extension using HTML JavaScript.

我需要访问此函数 VSS.getWebContext(),它为您提供当前运行扩展程序的项目页面的所有信息。

I need to access this function VSS.getWebContext() which gives you all information of the project page where you are currently running your extension.

let webContext = VSS.getWebContext();
let projectName = webContext.project.name + "_kpi_test"

# prints out fine
console.log('projname', projectName);

当我把它放入 onclick 函数时一个按钮,它工作正常并打印出我需要的信息。

When I put this into an onclick function of a button, it works fine and prints the information that I need.

但是当我只使用

    $(window).ready(function(){
        // your code
    }); 

    $(document).ready(function(){
        // your code
    }); 

window.onload = codeAddress; //my function here

使用 VSS.getWebContext()在页面加载时检索信息,它一直给我 VSS.getWebContext() undefined

to use the VSS.getWebContext() to retrieve the information when the page loads, it keeps giving me that VSS.getWebContext() is undefined.

如果它适用于onClick而不是页面加载,可能是什么问题?

If it works on onClick but not on page-load, what could be the problem?

推荐答案

我不确定VSS究竟需要什么,但由于它在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.

您可以尝试:

$(window).bind("load", function() {
   // code here
});

这篇关于当我把它放在onclick中但是当我尝试在页面加载时使用时它不起作用(JavaScript)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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