Javascript variable not able to display in HTML file
Take a look at my code :
<html>
<head>
<title>Profile App Example</title>
<script type="text/javascript" src="http://platform.linkedin.com/in.js">
api_key: some secret api key
authorize: true
</script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link media="all" type="text/css" href="http://developer.linkedinlabs.com/tutorials/css/jqueryui.css" rel="stylesheet"/>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.5b1.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.7/jquery-ui.min.js" ></script>
<script type="text/javascript">
function loadData() {
IN.API.Profile("me")
.fields(["id", "firstName", "lastName", "pictureUrl","headline"])
.result(function(result) {
profile = result.values[0];
/*
profHTML = "<p><a href=\"" + profile.publicProfileUrl + "\">";
profHTML += "<开发者_JAVA百科;img class=img_border align=\"left\" src=\"" + profile.pictureUrl + "\"></a>";
profHTML += "<a href=\"" + profile.publicProfileUrl + "\">";
profHTML += "<h2 class=myname>" + profile.firstName + " " + profile.lastName + "</a> </h2>";
profHTML += "<span class=myheadline>" + profile.headline + "</span>";
*/
profHTML = profile.id;
//alert(profHTML); alerts the id
//$("#profile").html(profHTML);
});
}
</script>
</head>
<body class="yui3-skin-sam yui-skin-sam">
<script>alert(profHTML);</script> <!-- Does not alert anything-->
<div id="profile">
</div>
<script type="IN/Login" data-onAuth="loadData"></script>
</body>
</html>
I am trying to capture the profile id in the HTML file , but unable to do that . How can I display it in the HTML file ? And why my code is not working . Please help
You need to create element with id='profile'
and uncomment $("#profile").html(profHTML);
line. $("#profile").html(profHTML)
is one of jQuery's the ways to 'alert' to HTML.
UPDATE: have you tried uncommenting $("#profile").html(profHTML);
? If yes, did you get any errors?
UPDATE 2: if you need to get those var out of the function, you can do the following:
var my_var = null;
function loadData() {
//.....
profHTML = profile.id;
my_var = profHTML;
//.....
}
If not - I can't understand what do you want to do with that variable. Please explain further. :)
UPDATE 3: There are two options:
- Do an AJAX request to your backend and save cookie. See JQuery.ajax and jQuery.cookie
Write that value to a hidden input in the login form, so this value will be submitted to your backend on form submit.
jQuery('#hidden').val(profile.id); //....somewhere in html....// <form> <!--your login form --> <input type='hidden' id='hidden' value=''> <!-- some other html --> </form>
I think you have the wrong scope of the variable. If you declare it outside of the function first, I think it will work.
精彩评论