0

I'm developing my website right now, the website will have an API made with Javascript and jQuery. In a game named ROBLOX I'm trying to get the player's username just via his ID. The only problem is that the username label has no ID, so I get the username label via the class.

However when I call innerHTML, innerText or call the .text() method, it doesn't return me anything, however there IS text in there. Is there another way to get the text inside the object? It's basically The class > h1 > text. I'm not sure if I'm using the wrong methods. Could someone help?

function My(){
    var user_id = 97423773
    var lawl = ""
    $.get("roblox.com/users/" + user_id + "/profile", function(data, success) {
        lawl = $(".profile-about-content-text", data).text()
        alert(lawl)
    });
    return lawl
}
My()
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
SomeDev
  • 109
  • 1
  • 2
  • 6
  • Any error msg in your console? – Zakaria Acharki Jan 19 '16 at 15:33
  • 2
    I hope you have used semicolons in your actual code! – Daniel PurPur Jan 19 '16 at 15:35
  • I'm a little confused. Wouldn't the username be a part of the profile you're querying and contained in the `data` object returned by `$.get()`? – Michael L. Jan 19 '16 at 15:47
  • @MichaelL. Yes but it's HTML sir, i wanna get the text from the data itself but it somehow isn't giving me the text – SomeDev Jan 19 '16 at 15:51
  • Also, for getting the class selector to work with a provided context, the node with the specified class must be a _child_ node in the returned document fragment, not the root node. Something like `
    username
    ` vs. '
    username
    `. This is because the selector will look _inside_ the root node, not at it.
    – Michael L. Jan 19 '16 at 16:40

1 Answers1

0

this is just working fine for me with semicolons added to the end of each line! your URL is a bit peculiar as well not ending with / or file extention

"roblox.com/users/"+user_id+"/profile/"
"roblox.com/users/"+user_id+"/profile.php"
"http://www.roblox.com/users/"+user_id+"/profile/index.php"
Daniel PurPur
  • 519
  • 3
  • 13
  • 1
    Given that `$.get` is asynchronous, I'm interested in how you've tested it – Rory McCrossan Jan 19 '16 at 15:50
  • @RoryMcCrossan I've actually added a settimeout 4 seconds, it still isn't working – SomeDev Jan 19 '16 at 15:57
  • I'm not sure how `setTimeout` relates to this? If you refer to the question I marked as a duplicate you need to execute all logic dependant on the result of the `$.get` call in the callback handler. – Rory McCrossan Jan 19 '16 at 15:59
  • I've used it because some people from the other replica of this thread have told me it's due to it being asynchronous, which means the program keeps on going even with the request still outbound – SomeDev Jan 19 '16 at 16:03
  • Rory is correct. The logic to handle the results need to be in the callback handler. Also, setTimeout does not 'pause'. It is also an asynchronous function. It continues normal code execution and then, after the specified delay, the function/code in the setTimeout is added to the event loop to be executed. – Michael L. Jan 19 '16 at 16:03
  • @MichaelL. Could you please link me to a good thread/question explaining that?! – SomeDev Jan 19 '16 at 16:05
  • function My(){ var user_id = 97423773 var lawl = "" $.get("roblox.com/users/"+user_id+"/profile", function(data,success){ lawl = $(".profile-about-content-text", data).text(function(){ return lawl }); } My() would this work? – SomeDev Jan 19 '16 at 16:12
  • This blog post by John Resig (who created jQuery) is a primer on how setTimeout works: http://ejohn.org/blog/how-javascript-timers-work/ – Michael L. Jan 19 '16 at 16:13
  • @RbxDealsOwner You don't want to pass a function into `.text()`. The 'callback' is the function passed as the second parameter to `$.get`. Everything you want to do with `data` needs to be handled within that function. Also, because it is asynchronous, you can't 'return' a value from that function to the 'calling code'. So instead of trying to return the value, you would want to grab it from the data and then immediately do something with it or pass it to another function. – Michael L. Jan 19 '16 at 16:17
  • @RbxDealsOwner Also, take a look at this tutorial explaining asynchronous code in JS: http://code.tutsplus.com/tutorials/event-based-programming-what-async-has-over-sync--net-30027 – Michael L. Jan 19 '16 at 16:20
  • Hi All I have put alerts and return a dummy test and the code works fine buy putting semicolons. not if the get doesnot return anything that is either URL or something relating to the server that we do not have the access to the code. – Daniel PurPur Jan 19 '16 at 16:27
  • @MichaelL. I've taken your advice highly into appreiciation, and tried working something out, however it stills alerts with emptiness function Alert(Arg){ alert(Arg) }; function My(){ alert("ok"); var user_id = 97423773; var lawl = ""; $.get("roblox.com/users/"+user_id+"/profile", function(data,success){ lawl = $(".profile-about-content-text", data).text(); Alert(lawl); }) } – SomeDev Jan 19 '16 at 16:27
  • @RbxDealsOwner If this is the actual code then I will update my answer. let me know please. – Daniel PurPur Jan 19 '16 at 16:30
  • @DanielPurPur Yessir. – SomeDev Jan 19 '16 at 16:31
  • By the way guys, when i tested this in an extension or console, it worked, it just wouldn't work on my website – SomeDev Jan 19 '16 at 16:32
  • that is why I am saying it is a url or server matter. your code looks fine to me.. obviously with semicolons! included – Daniel PurPur Jan 19 '16 at 16:37
  • Also see my updated answer regarding the URL please. – Daniel PurPur Jan 19 '16 at 16:43
  • @RbxDealsOwner Did you see my last comment on the original post, noting that the node with the class must be a _child_ of the root and _not_ the root itself, for the `$('.class-name', context).text()` jQuery syntax to work? Also, don't create your own `Alert` function, just use the standard `alert` function. – Michael L. Jan 19 '16 at 19:18
  • Also, @DanielPurPur, to Rory's point, could you post a link to an example of it 'working with semicolons'? You _might_ have gotten the alert to work, but I don't see how the `return lawl` statement could work, given that `$.get()` is asynchronous. – Michael L. Jan 19 '16 at 19:21
  • I have used a different url (my own) but the same structure, hence saying this could well be a problem on the server side code. One thing I would like to know is ... Do you see any errors in syntax apart from the ones I mentioned? – Daniel PurPur Jan 20 '16 at 09:29