Reputation: 401
I would like to build in error handling for this piece of javascript so that if #next doesnt exist that it will do something else. Currently when it doesnt exist it just gives me an undefined in the url or an error page. Please let me know what i can do here. thanks
<script>
$(document).on("pageinit",function(){
var pathname = $(location).attr('href');
var leva = $('#next').attr("href");
var prava = $('#prev').attr("href");
$("body").on("swiperight",function(){
<!--alert("You swiped right!");-->
location.href=prava;
});
$("body").on("swipeleft",function(){
<!--alert("You swiped left!");-->
location.href=leva;
});
});
</script>
Upvotes: 0
Views: 58
Reputation: 19619
You can check $('#next').length
to see if any elements were selected. Or just check if leva
is undefined
.
Something like this:
<script>
$(document).on("pageinit",function(){
var pathname = $(location).attr('href');
var leva = $('#next').attr("href");
var prava = $('#prev').attr("href");
$("body").on("swiperight",function(){
<!--alert("You swiped right!");-->
location.href=prava;
});
$("body").on("swipeleft",function(){
<!--alert("You swiped left!");-->
if(leva) {
location.href=leva;
} else {
// Some other action
}
});
});
</script>
Upvotes: 1