I want to display user profile when admin mouser over on username link. If this is first time, user profile is displayed; then next time ajax should not fire and display user profile without ajax fire.
-
Show us some code. what have you tried. – Catfish Mar 06 '13 at 18:31
-
Why? do you want to give Vote Down – Mar 06 '13 at 18:32
-
I have to implement this functionality but i dont know how to implement it – Mar 06 '13 at 18:34
-
^ What have you tried ?? – Rahul garg Mar 06 '13 at 18:35
-
I have just created a html view with link on username – Mar 06 '13 at 18:36
-
Can you show us your view code? Instead of ajax, can you just hide a div and show it when hovered? – Catfish Mar 06 '13 at 18:39
-
@Catfish I assume the image loading has to be dynamic corresponding to each user. – Jonast92 Mar 06 '13 at 18:44
-
Like Rahul pointed it, It's hard to give a full working code to such a broad question. but Nich and I have both bothered to give you an example, take a look at them. – Jonast92 Mar 06 '13 at 18:58
3 Answers
You need to know the id and the class of the username link.
You can make jQuery listen to the hover, when that event occurs you can call the function which will do the ajax.
But, you need to know the id of the user, the best way to do that is to do something like
<a href='user123.php' class='userHref' id='user_123'>I want to be hovered</a>
Now you have a link to hover over.
$('.userHref').live("hover", function()
{
var userHrefId = $(this).attr('id');
var userHrefIdSplit = userHrefId .split('_');
var userId = userHrefIdSplit[1];
useAjax(userId);
});
Now you have listened to the hover by listening to any hovering over a link of the class userHref, jquery has responded by taking the id of the a element, splitting the id into 2 seperate items, where the second one indicates the user Id.
Now we have also called with useAjax function and we have sent the id of the user. Now you can POST the userId to a known backend site (rails in your example), which will query the database and return the url to the user image. We then only have to know the id of the div element which you want the image to appear in.
function useAjax(userId);
{
var id = userId;
var select = true;
var url = '../scripts/ajax.php';
$.ajax(
{
// Post select to url.
type : 'post',
url : url,
dataType : 'json', // expected returned data format.
data :
{
'select' : select, // the variable you're posting.
'userId' : id
},
success : function(data)
{
// This happens AFTER the backend has returned an JSON array
var userUrl, userImg, message;
for(var i = 0; i < data.length; i++)
{
// Parse through the JSON array which was returned.
// A proper error handling should be added here (check if
// everything went successful or not)
userUrl = data[i].userUrl;
message = data[i].message;
userImg = "<img src='"+userUrl+"' alt='' title='' />";
$('#someDiv').html(userImg); // Here's your image.
}
},
complete : function(data)
{
// do something, not critical.
}
});
}
I'm not familar with rails but you can probably program the backend in a similar wasy as I exmplained here: Javascript function as php?
Search for my answer, it should give you a very detailed example.
I hope this helps.
Tip for the future: Try and google first :)
Assuming you're using jQuery, bind a hover event to the user name link. As so:
$('.username').hover(function (e) {
console.log("i'm hovering!! on id: "+$(this).attr('data-user-id')); //See the next step for where this came from
}
Next, add the user's id to the username element, perhaps in a data attribute:
<span class="username" data-user-id="1234567890">Username</span>
Next, keep a record of which users are already loaded, perhaps by id. When you fetch something new, add it to the object. I like to keep objects like this on the window.
window.loadedUserInfo = {};
On hover check if the userId key exists in this object. If it does, user it. If not, use an ajax call to get it:
$.ajax({
url : "path/to/userinfo"+userid, //I'm assuming you're using restful endpoints
type : "GET",
success : function (res) {
window.loadedUserInfo[userid] = res;
//Format your popover with the info
},
error: function (jqxhr) {
//something went wrong
}
})
As for the popover itself, you could probably use a bootstrap popover.
Putting it all together:
$(".username").hover(function (e) {
console.log("i'm hovering!! on id: "+$(this).attr("data-user-id")); //See the next step for where this came from
if (typeof window.loadUserInfo[$(this).attr("data-user-id")] == 'undefined') {
$.ajax({
url : "path/to/userinfo"+userid, //I'm assuming you're using restful endpoints
type : "GET",
success : function (res) {
window.loadedUserInfo[userid] = res;
//Format your popover with the info
},
error: function (jqxhr) {
//something went wrong
}
})
} else {
//populate popover with info in window.loadUserInfo[$(this).attr('data-user-id')]
}
}

- 5,452
- 1
- 25
- 31
-
-
Because his requirement is that he is making a call to GET user info from his server? – Nick Mitchinson Mar 06 '13 at 18:52
-
GET should be avoided, he can easily POST the id to a backend which will return him json output which he can work with. – Jonast92 Mar 06 '13 at 18:54
-
Both will work fine, and it comes down to implementation. They will both do the same thing in the end. Why do you say GET should be avoided? A POST request can just as easily be faked, and access control can just as easily be implemented on a get request. – Nick Mitchinson Mar 06 '13 at 18:57
-
By using GET you're increasing the length of the url, making it visible to the user, perhaps too visible. Complex urls are bad for SEO and IT's easy for the user to modify the url, therefor making it very easy for hackers to crossite script the website if it isn't handled properly. – Jonast92 Mar 06 '13 at 19:02
-
-
That is true, just a few more comments. Afterwards we can move this to a chat if you still want to discuss this. These endpoints (user information) should be private information, and thus SEO does not apply. Those endpoints (and links to them) should not be indexable (or even findable for that matter). Also, hackers can just as easily modify a POST request, using POST does not in any way make your requests safer. You need to protect again XSS attacks either way. – Nick Mitchinson Mar 06 '13 at 19:14
-
True, but It's just a matter of which way is more of a pain in the add to do. But I totally agree with you, both ways require protection. I think we can leave it at that. – Jonast92 Mar 06 '13 at 19:28
To implement the functionality proceed step by step:
- On mouseover of the username, implement an ajax call that renders user profile in html near the username
- Through javascript, implement functionality such that when user leaves the username/userprofile, the user profile div is now hidden
- While making ajax calls in #1 above, check if the div already exist which contains user profile for the userid which you are trying to request. This can be easily achieved by having some
id
in the user profile part and checking if that#user_profile_#{id}
div exists.
Your requirement is too broad to be able to provide any code... If you have problem in implementation of any of the above parts, post them as question separately one by one..

- 9,202
- 5
- 34
- 67