0

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

4
  • Have you checked your console for errors? Commented May 19, 2016 at 19:37
  • 1
    Instead of .onclick = onLinkedInLoad try .addEventListener('click', onLinkedInLoad) Commented May 19, 2016 at 19:38
  • have you tried <button id="btn" onclick="onLinkedInLoad()">Try Me</button> Commented May 19, 2016 at 19:38
  • Thank you a lot for the answers. I just made an update on my question Commented May 19, 2016 at 19:44

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Do you maybe have time to give me a link to the fiddle? I would be really happy about that. Best regards
Here is the link. jsfiddle.net/w3pjuxd6/
Thank you a lot. What I am really in doubt is. As it is now I call an alert. But I have to call the LinkedIn API, so I get the share function. Am I on the wrong path here?
For the first error, try to extract all your Javascript code to an external file, and load it after document ready. For the second error, is the path (/people/~/shares?format=json) you are using correct? Maybe using the full path may work, but I'm not sure.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.