简单例子
<html>
<head>
<title>js微博</title>
<script src=" http://tjs.sjs.sinajs.cn/open/api/js/wb.js?appkey=4079112329" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
</head>
<body>
<div position="fixed"><button>登录</button> </div>
<div id="content"></div>
<script>
function login()
{
if(WB2.checkLogin()) //用户是否否登录
{
//获取用户微博信息
$("button").text("退出");
getStatus();
}
else
{
WB2.login(
function()
{
$("button").text("退出");
getStatus();
}
);
}
}
function logout()
{
WB2.logout(
function()
{
$("button").text("登录");
$("#content>div").remove();
}
);
}
//获取当前登录用户及其所关注用户的最新微博消息
function getStatus()
{
var url="/statuses/friends_timeline.json";
WB2.anyWhere(
function(W)
{
W.parseCMD(
url,
function(result,b)
{
for(i=0;i<result.length;i++)
{
username=result[i].user.name;//微博作者
userimg=result[i].user.profile_image_url;//微博作者头像
date=result[i].created_at; //微博发布时间
id= result[i].id;//微博id
text=result[i].text;//微博内容
img=result[i].thumbnail_pic;//微博配图
if(result[i].retweeted_status!=null)
{
username1=result[i].retweeted_status.user.name;
text=text+"@"+username1+"<br>"+result[i].retweeted_status.text;//转发内容
img=result[i].retweeted_status.thumbnail_pic;//微博配图,小图
}
if(img!=null)
{
text=text+"<br><img src=\""+img+"\" />";
}
message="<img src=\""+userimg+"\"> "+username+":"+text;
$("#content").append("<div>"+message+"</div>");
}
}
);
}
);
}
$("button").click(
function()
{
if($(this).text()=="登录")
{
login();
}
if($(this).text()=="退出")
{
logout();
}
}
)
</script>
</body>
</html>