Calling JS function onClick - javascript

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.

Related

Google OAuth2 Callback empty

I am attempting THIS OFFICIAL GOOGLE EXAMPLE and am running into an empty object/array on the sign in callback. The ONLY modifications I have made to this from Google's examples are the console.logs the alerts and a small syntax error they had
I changed
auth2.grantOfflineAccess().then(signInCallback);
to
auth2.grantOfflineAccess().then(signInCallback());
Without the () the function wouldn't fire. and the made that alert would not display. Now having copied this DIRECTLY from Google's page, have I missed something or is there another bug that I missed?
Also worth noting -- The origins are set up:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html itemscope itemtype="http://schema.org/Article">
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script src="https://apis.google.com/js/client:platform.js?onload=start" async defer></script>
<script>
function start() {
gapi.load('auth2', function () {
auth2 = gapi.auth2.init({
client_id: 'xxxxxxxx.apps.googleusercontent.com',
scope: 'https://www.googleapis.com/auth/plus.business.manage'
});
});
}
</script>
</head>
<body>
<button id="signinButton">Sign in with Google</button>
<script>
$('#signinButton').click(function () {
// signInCallback defined in step 6.
alert('made this');
let test = auth2.grantOfflineAccess().then(signInCallback());
console.log(test);
});
function signInCallback(authResult) {
alert('made that');
console.log(authResult);
if (authResult['code']) {
// Send the code to the server
} else {
// There was an error.
}
}
</script>
</body>
</html>
My console.log(authResult); just inside the signInCallback() function is empty -> undefined. I am able to get my user logged in and the pop-up indicates all scopes are granted. Why am I not able to grab the access token?

Failing to get simple SoundCloud javascript api method to work

I have the following code below :
<!DOCTYPE html>
<html>
<head>
<script src="http://connect.soundcloud.com/sdk.js"></script>
<script>
SC.initialize({
client_id: "f520d2d8f80c87079a0dc7d90db9afa9"
});
SC.get("/users/3207",{}, function(user){
console.log("in the function w/ " + user);
});
</script>
</head>
</html>
The code should print the user name to the console however whenever I run this, my console gives the error of :
Failed to load resource: The requested URL was not found on this server:
file://api.soundcloud.com/users/3207?client_id=f520d2d8f80c87079a0dc7d90db9afa9&format=json&_status_code_map%5B302%5D=200
However if I were to directly http://api.soundcloud.com/users/3207.json?client_id=f520d2d8f80c87079a0dc7d90db9afa9, then I get a valid JSON result.
Is there something incorrect with my how I am using the SC.get function?
Thanks
Well, you should test your index.html locally on a web-server like Apache and not by opening it as a file.
Working example
SC.initialize({
client_id: "f520d2d8f80c87079a0dc7d90db9afa9"
});
SC.get("/users/3207", {}, function(user) {
console.log("in the function w/ " + JSON.stringify(user));
var res = document.getElementById("result");
res.innerHTML = JSON.stringify(user);
});
<script src="http://connect.soundcloud.com/sdk.js"></script>
<div id="result"></div>

unable to call function in dojo module from another dojo module

I'm getting an Object doesn't support property or method error when I try to call a function in a dojo module. I have a main page and two modules. I call the first module from the main page and it works, I call the second module from the first and it works, but I get the error when I try to call the first module from the second. Here is my code:
main page:
<!DOCTYPE html>
<html >
<head>
<meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no"/>
<link rel="stylesheet" href="http://js.arcgis.com/3.10/js/dojo/dijit/themes/claro/claro.css">
<link rel="stylesheet" href="http://js.arcgis.com/3.10/js/esri/css/esri.css">
<script>
var dojoConfig = {
parseOnLoad:true,
async: true,
isDebug:true,
packages: [
{name: "Scripts", location: location.pathname.replace(/\/[^/]+$/, "") + "/Scripts"},
]
};
</script>
<script> src="http://js.arcgis.com/3.10/"></script>
<script>
require(["Scripts/Mod1", "Scripts/Mod2"],
function (Mod1, Mod2) {
Mod1.M1Method("call from main page");//works great
});
</script>
</head>
<body class="claro">
<div>look here you</div>
</body>
</html>
Module 1:
define(["Scripts/Mod2"],
function (Mod2) {
return {
M1Method: function (msg) {
alert(msg);
Mod2.M2Method("call from Mod1");//works great
},
M1Method2: function (msg) {
alert(msg);
}
}
});
Module 2:
define(["Scripts/Mod1"],
function (Mod1) {
return {
M2Method: function (msg) {
alert(msg);
Mod1.M1Method2("call from Mod2"); //JavaScript runtime error: Object doesn't support property or method 'M1Method2'
}
}
});
How can I make that call from Mod 2 to Mod1?
Thanks
You are trying to do a cyclic dependency. In module 2, use a require statement. Try something like this:
define(["require"],
function (require) {
return {
M2Method: function (msg) {
alert(msg);
try {
require(["Scripts/Mod1"], function(Mod1) {
Mod1.M1Method2("call from Mod2");
});
} catch (dohObj) {
alert('Doh!, this failed. Stupid answer: ' + dohObj.message);
}
}
}
}
);
Your html code has a typo in that you enclosed the 'script' tag too soon when including dojo.js. Should be something like
<script type="text/javascript" src="http://js.arcgis.com/3.10/"></script>
You need to fix that file first to properly include dojo and 'require()'.

Uncaught TypeError: number is not a function : While create dojo class

I am learning dojo
i have created a dojo class using declare as below
require(
["dojo/_base/declare"],function(declare){
return declare(null,{
constructor : function(){
alert("done");
}
});
});
and this is save in /learnDojo/root/test.js
and the index.html as below
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<script type="text/javascript">
var dojoConfig = {
async: false,
parseOnLoad: false,
isDebug : true,
packages:[
{ name:"base",
location:"/learnDojo/root"
}]
};
</script>
<script src="//localhost:8080/dojo1.9.0/dojo/dojo.js" type="text/javascript"></script>
<script type="application/javascript">
require(["base/test","dojo/_base/declare","dojo/domReady!"],function(test,declare){
var test = new test();
});
</script>
<body>
</body>
</html>
i have the index.html in the /learnDojo which is in webapps folder of the tomcat
When i load this from browser i am getting the error "Uncaught TypeError: number is not a function"
Why? any issue with code or dojo syntax for creating the class
When defining Dojo modules you should be using define(), not require(). So your /learnDojo/root/test.js file would look like this:
define(
["dojo/_base/declare"],function(declare){
return declare(null,{
constructor : function(){
alert("done");
}
});
});
The reason for this is that define() will actually return the object/value you returned in your callback (in this case the return declare(...) statement. A require() however will not return that value, causing strange errors.

Logout from LinkedIn API

I have worked with LinkedIn API and need guidance about logging out from Linkedin API once I click Logout in my application. I have tried the following code:
function closeSession(){
IN.User.logout();
}
Logout In LinkedIn
I have also tried:
$('logout-link').click(function() {
IN.Event.on(IN,'logout', function() {
window.location.href = [site-logout-url];
});
IN.User.logout();
});
I tried to destroy the session in a browser by calling session_destroy()
Finally, I tried the answer I got from Stack Overflow for this question. I didn't get any right solution. Can anyone tell me the solution?
Make sure that you have include
<script type="text/javascript" src="http://platform.linkedin.com/in.js"></script>
on your home page, i.e. where you are logging out.
Seems your onclick is not being called, try doing:
function closeSession(){
IN.User.logout();
return true;
}
Logout In LinkedIn
OR
<a href="#" class="myButton" onclick="closeSession()" id="logout-link">
Logout In LinkedIn
</a>
And js
function closeSession() {
if ( typeof IN === 'object' && typeof IN.User === 'object' && IN.User.isAuthorized() ) {
//user logged in linkedin
//call linkedin logout with websites logout function as callback
alert("I m inside linkedin object check.."); //do you see this
IN.User.logout(logout);
}
else {
logout();
}
}
function logout() {
window.location.href = "URL_TO_YOUR_LOGOUT.PHP";
}
I think you are redirected before your javascript gets executed, so try this way ?
JavaScript Code :
function closeSession(){
IN.User.logout();
location.href="logout.php";
}
HTML Code :
<span style="cursor:pointer" onclick="closeSession()">Logout In LinkedIn</span>
To make it work
<script type="text/javascript" src="http://platform.linkedin.com/in.js">
api_key: <?php echo sfConfig::get('app_linkedin_api_key'); ?>
authorize: true
</script>
<script>
IN.Event.on(IN, "logout", function() {onLinkedInLogout();});
function onLinkedInLogout(){
// User is logged out
window.location.href='<?php echo url_for("#homepage");?>'
}
</script>
<?php echo __("Logout");?>
<div class="signin"><script type="in/Login" data-onAuth="onLinkedInAuth"></script>
<script type="text/javascript">
function onLinkedInAuth() {
IN.API.Profile("me")
.fields("id")
.result( function(me) {
var id = me.values[0].id;
//Do stuff like redirect...
})
}
</script>
There seems to be 2 different issues here. You can't logout of linked in from your server side script unless you call their function. When you land on logout.php you should have your session_destroy(); on top of the page. Make sure there isn't any session_start() on logout.php
You need to load the LinkedIn JavaScript platform before trying to call IN.User.logout(), you should run the code only after the javascript library has loaded completely.
<script type="text/javascript" src="http://platform.linkedin.com/in.js">
api_key: mykey
authorize: true
onLoad: onLoad
</script>
<script type="text/javascript">
function onLoad() {
try {
IN.User.logout();
} catch (err) {
console.log(err);
}
location.href="index.php";
}
</script>
<script type="text/javascript" src="http://platform.linkedin.com/in.js">
api_key: mykey
authorize: true
onLoad: onLoad
</script>
<script type="text/javascript">
function onLoad() {
try {
IN.User.logout();
} catch (err) {
console.log(err);
}
location.href="index.php";
}
</script>
use session_destroy() in the code.

Categories

Resources