I have little trouble understanding why variables aren’t working properly in the following code.
var welcomeMsg = $('#accountbar span.welcome_text').text(); // This returns "Welcome, Nick(account) "
var cutdown = welcomeMsg.slice(0, -6); // This removes excessive spaces after (account)
cutdown = cutdown.slice(cutdown.indexOf(",")+2,cutdown.indexOf("(")); // returns "Nick"
var username = cutdown; // "Nick" //This doesn't work with last line of the script
//alert(username);
//var username = "Nick" // but this works if it's used instead of var username = cutdown;
//alert(username);
$("td:contains('"+username+"')").parent().css({
"background" : "url('http://imagehost.com/forum/row1.png')"
});
It’s for a Greasemonkey script, if that matters.
Solved:
It seems that .trim is better way to delete excessive spaces around the wanted string.
The script works perfectly for me if this
var cutdown = welcomeMsg.slice(0, -6); // This removes excessive spaces after (account)
cutdown = cutdown.slice(cutdown.indexOf(",")+2,cutdown.indexOf("("));
is replaced with this
var cutdown = welcomeMsg.slice(welcomeMsg.indexOf(",")+2,welcomeMsg.indexOf("("));
cutdown = cutdown.trim();
.trimfixed the problem when it is used instead of.slice