简体   繁体   English

调用JS函数onClick

[英]Calling JS function onClick

I am trying to call this Javascript function. 我正在尝试调用此Javascript函数。 When I hit my button, there is nothing loading. 当我按下按钮时,没有任何负载。 I set an id for my button. 我为按钮设置了ID。 With the onclick I load the function onLinkedInLoad. 使用onclick时,我会加载onLinkedInLoad函数。 But is it not that function I should call to activate the script? 但是,不是我应该调用该函数来激活脚本的功能吗? Can anybody see what I am doing wrong here? 有人可以在这里看到我在做什么吗?

<!DOCTYPE html>
    <html>
    <head>
        <title></title>
        <meta charset="utf-8" />
    </head>
    <body>
        <button id="btn">Try Me</button>

        <script type="text/javascript">
            document.getElementById("btn").onclick = onLinkedInLoad;
        </script>

        <script type="text/javascript" src="//platform.linkedin.com/in.js">
      api_key: myKey
      authorize: true
      onLoad: onLinkedInLoad
        </script>
        <script type="text/javascript">

      // Setup an event listener to make an API call once auth is complete
        function onLinkedInLoad() {
          IN.Event.on(IN, "auth", shareContent);
        }

      // Handle the successful return from the API call
      function onSuccess(data) {
        console.log(data);
      }

      // Handle an error response from the API call
      function onError(error) {
        console.log(error);
      }

      // Use the API call wrapper to share content on LinkedIn
      function shareContent() {

        // Build the JSON payload containing the content to be shared
        var payload = {
          "comment": "Check out developer.linkedin.com! http://linkd.in/1FC2PyG",
          "visibility": {
            "code": "anyone"
          }
        };

        IN.API.Raw("/people/~/shares?format=json")
          .method("POST")
          .body(JSON.stringify(payload))
          .result(onSuccess)
          .error(onError);
      }

        </script>

    </body>
    </html>

Update: I get this in my console: 更新:我在控制台中得到了这个:

18 Uncaught ReferenceError: onLinkedInLoad is not defined
www.linkedin.com/uas/js/userspace?v=0.0.1191-RC8.56309-1429&apiKey=myKey&authorize=true&onLoad=onLinkedInLoad&secure=1&:
22 Uncaught Error: You must specify a valid JavaScript API Domain as part of this key's configuration.

I putted my APIkey in also. 我也放了APIkey。 I just replaced in the code here with "myKey". 我只是在这里用“ myKey”替换了代码。 I also tried with .addEventListener('click', onLinkedInLoad) , but still nothing happens 我也尝试了.addEventListener('click', onLinkedInLoad) ,但仍然没有任何反应

Try to load the function definition script before the call script. 尝试在调用脚本之前加载函数定义脚本。 Or better, take all the scripts in another file and just load that on your HTML. 或者,最好将所有脚本保存在另一个文件中,然后将其加载到HTML中。

I've tried your code on js fiddle and it works by doing that. 我已经在js小提琴上尝试过您的代码,并且可以这样做。

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

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