简体   繁体   中英

Calling JS function onClick

I am trying to call this Javascript function. When I hit my button, there is nothing loading. I set an id for my button. With the onclick I load the function 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. I just replaced in the code here with "myKey". I also tried with .addEventListener('click', onLinkedInLoad) , but still nothing happens

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.

I've tried your code on js fiddle and it works by doing that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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