2016-05-19 104 views
0

我想调用这个Javascript函数。当我点击我的按钮时,没有任何加载。我为我的按钮设置了一个ID。使用onclick我加载函数onLinkedInLoad。但是,这不是我应该打电话来激活脚本的功能吗?任何人都可以看到我在这里做错了吗?调用JS函数onClick

<!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> 

更新: 我得到这个在我的控制台:

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. 

我的推杆我APIkey中也。我在这里用“myKey”替换了代码。我也尝试过.addEventListener('click', onLinkedInLoad),但仍然没有任何反应

+0

你检查了你的控制台是否有错误? –

+1

而不是'.onclick = onLinkedInLoad'尝试'.addEventListener('click',onLinkedInLoad)' – mpallansch

+0

您尝试过吗?试试我 –

回答

1

尝试在调用脚本之前加载函数定义脚本。或者更好的是,将所有脚本放在另一个文件中,并将其加载到HTML上。

我试过你的js小提琴上的代码,它的工作原理是这样的。

+0

非常感谢。我现在就试试 – M375

+0

你有没有时间给我一个小提琴的链接?我会很高兴。最好的问候 – M375

+0

这里是链接。 jsfiddle.net/w3pjuxd6/ –