- Aug 7, 2001
- 8,680
- 3
- 0
I'm trying to set a form field to the value of a GET variable (if set) called destination:
function destinationFunction() {
var searchString = document.location.search.split("destination=");
var searchSplit = searchString[1].split("&");
document.getElementById('destination').value = searchSplit[0];
}
This function works fine when destination is set in the URL but "crashes" when it isn't (which is fine, since I don't need it to do anything in that case). I'm sort of a JavaScript noob, so I was wondering if it's a bad idea to do it this way even though the end result is correct. I believe it crashes because searchString[1] doesn't exist when destination isn't set in the URL. Also in the first line, I'm splitting a string that may not exist, and even if it does exist, it may not have "destination=" in it. Is it okay to do this? Obviously I could throw a bunch of conditionals in, but is it even worth worrying about?
Thanks
function destinationFunction() {
var searchString = document.location.search.split("destination=");
var searchSplit = searchString[1].split("&");
document.getElementById('destination').value = searchSplit[0];
}
This function works fine when destination is set in the URL but "crashes" when it isn't (which is fine, since I don't need it to do anything in that case). I'm sort of a JavaScript noob, so I was wondering if it's a bad idea to do it this way even though the end result is correct. I believe it crashes because searchString[1] doesn't exist when destination isn't set in the URL. Also in the first line, I'm splitting a string that may not exist, and even if it does exist, it may not have "destination=" in it. Is it okay to do this? Obviously I could throw a bunch of conditionals in, but is it even worth worrying about?
Thanks