javascript - preventDefault stops window.location.href from working -
my site has 2 ads on it, 1 of them uses script parses links on site , preventdefault()
on them... makes impossible me use preventdefault() on links since can done once... in past i've been able work around using return false
. reason doesn't work jquery ui autocomplete function though (see code below).
if turn off ads script works fine. otherwise reloads page because return false;
doesn't seem work...
script cannot change:
$(document).ready(function() { $('a').on('click', function(e) { e.preventdefault(); // stuff }); )};
my script:
$search.autocomplete({ source: function(request, response){ $url = "suggestions/"; $.get($url, {data:request.term}, function(data){ response($.map(data, function(item) { return { label: item.movie_name, id: item.movie_id } })) }, "json"); }, minlength: 2, datatype: "json", cache: true, focus: function(event, ui) { return false; }, select: function(event, ui) { window.location.href = ('/id/'+ ui.item.id +'/'+ ui.item.label +'/'); return false; } });
i found solution works:
select: function(event, ui) { $('.ui-autocomplete a').attr('href', '/id/'+ ui.item.id +'/'+ ui.item.label +'/'); }
explaination: jquery-ui-autocomplete function creates list of links ()-tags, use preventdefault()
stop browser going through href address, since in case not possible tried using return false
instead since doesn't work either in case figured, why not change link-tags href correct url instead , not halt action, works!
Comments
Post a Comment