Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialAndrew McCombs
4,309 PointsI have a question about javascript - Code Help
Okay so this is what I would like to do.
I have a text field where you would put in a nickname and that nickname is for the URL that I need to send them to. We the URL is a we link to a application on your desktop.
Teamspeak ts3server://216.127.64.131/?port=3398&nickname="NICKNAME
Well im using java script to check to make sure that is a username the taking the information that is in the field and to open the client up on the computer.
function connectts3(){
var n = _("nickname").value;
var status = _("status");
if(n == ""){
status.innerHTML = "You have to enter a nick name.";
} else {
_("connectbtn").style.display = "none";
status.innerHTML = 'please wait ...';
window.location("ts3server://216.127.64.131/?port=3398&nickname="+n);
}
}
//How would I put this in the code to say if there was a problem and the
//window.location didnt do anything then to say this
//
//status.innterHTML = "There was a problem connecting to the server, Please open your Teamspeak Client, and enter the server information manually. Server Address: ts3.BGUnited.us";
and the form
<br><br><div id="status"></div><form action="" onsubmit="return false"><input class="form-control" id="nickname" type="text" placeholder="Nickname"><br><input type="submit" id="connectbtn" onclick="connectts3()" value="Connect to Server" class="btn btn-primary"></form></p>
Roy Oostwouder
8,720 PointsWould you mind me asking why you use the underscore prefix like _("nickname").value;
?
Andrew McCombs
4,309 PointsBecause, I have a text field that people are entering the nickname. And then it adds it to the window.location
3 Answers
Lucas Krause
19,924 PointsGenerally you can't figure out whether the location redirected to is valid or not. However, you can be sure that settings window.location
(or window.location.href
) will redirect you (regardless of whether the address is valid or not). Moreover you should use encodeURIComponent()
and do something like:
window.location.href="ts3server://216.127.64.131/?port=3398&nickname="+encodeURIComponent(n);
You could use setTimeout()
. Here's an example:
function connectts3(){
var n = _("nickname").value;
var status = _("status");
if(n == ""){
status.innerHTML = "You have to enter a nick name.";
} else {
_("connectbtn").style.display = "none";
status.innerHTML = 'please wait ...';
window.location.href="ts3server://216.127.64.131/?port=3398&nickname="+encodeURIComponent(n);
setTimeout(function(){
alert("You didn't get redirected yet!"); //this code will never be executed if the redirection happens with 5000ms
}, 5000); //5000ms
}
}
Roy Oostwouder
8,720 PointsWould you mind me asking why you use the underscore prefix?
Lucas Krause
19,924 PointsDo you mean _("nickname").value
for example? I just copied the code from above. You should ask Andrew McCombs instead. ;)
Roy Oostwouder
8,720 PointsAs far as I know Javascript cannot check whether the location you want to go to is valid. But if in some way you want to check something before running the window.location you should build in an if statement as you probably already know:
} else {
_("connectbtn").style.display = "none";
status.innerHTML = 'please wait ...';
if (condition you want to check) {
window.location("ts3server://216.127.64.131/?port=3398&nickname="+n);
}
}
Andrew McCombs
4,309 PointsThis is there error when the window.location is executed - TypeError: object is not a function
Roy Oostwouder
8,720 PointsI was completely focused on seeing whether the window.location was valid, but window.location is not a function so you can't use window.location(url); The parenthesis imply a function which it isn't.
You should try window.location = "url";
notf0und
11,940 PointsI believe you're using window.location
incorrectly. MDN says there are two ways of doing this:
window.location = "http://website.com";
# Or
window.location.assign("http://website.com");
Maybe that will fix it? I'm honestly not too sure :/
Roy Oostwouder
8,720 PointsThat's what I thought. Don't forget the semicolons at the end though :)
notf0und
11,940 PointsHa, thanks for pointing that out. Too much Ruby and Python for me :P
notf0und
11,940 Pointsnotf0und
11,940 PointsI've edited your post so that the JavaScript shows up correctly. Press edit to see the changes, or check out this post for how to place code on the forum: https://teamtreehouse.com/forum/posting-code-to-the-forum