preventDefault stops window.location.href from working

OZZIE

My site has two ads on it, one of them uses a script which parses all links on the site and does preventDefault() on them... this makes it impossible for me to use preventDefault() on any links since it can only be done once... However in the past I've been able to work around this using return false. For some reason it doesn't work with the jQuery ui autocomplete function though (see code below).

If I turn off the ads the script works fine. Otherwise it just reloads the page because the return false; doesn't seem to work...

Script I 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;
  }
});
OZZIE

I found a solution that works:

  select: function(event, ui) {
     $('.ui-autocomplete a').attr('href', '/id/'+ ui.item.id +'/'+ ui.item.label +'/');     
  }

Explaination: The jquery-ui-autocomplete function creates a list of links ()-tags, normally you would use preventDefault() to stop the browser from going through to the href address, since in my case that is not possible I tried using return false instead but since that doesn't work either in this case I finally figured, why not change the link-tags href to the correct url instead and not halt the action, which works!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Variable from function into window.location.href 'not defined'

From Dev

onclick window.location.href with input value

From Dev

String is not a function on window location href

From Dev

jQuery preventDefault() stops onclick from working

From Dev

Javascript window.location.href onClick not working

From Dev

window.location.href not working in IE 11

From Dev

Enable and disable window.location.href

From Dev

Redirect a Website to a local folder Using window.location.href() not working

From Dev

window.location = '' is not working ;

From Dev

Event.preventDefault stops php script from submitting correct values

From Dev

$window.location.href NOT WORKING in AngularJS

From Dev

window.location.href not working

From Dev

window.location.href and location.reload() does not load updated page from Server

From Dev

Make window.location.href load page from Server instead from browser cache

From Dev

JavaScript window.location.href not working in localhost

From Dev

window.location.href not working, mod rewrite or jquery?

From Dev

window.location.href not working on IE

From Dev

window location not working javascript

From Dev

location.href not working

From Dev

window.location.href and hash are not working on chrome

From Dev

Javascript window.location.href onClick not working

From Dev

window.location.href not working after Response.End()

From Dev

window.location.href not working as expected

From Dev

Make window.location.href load page from Server instead from browser cache

From Dev

window.location.href isn't working

From Dev

`window.location.href` not working as intended

From Dev

Any other ways apart from Window.location.href?

From Dev

*ngIf window.location.href.indexOf is not working

From Dev

angular4 unit test for window.location.href redirect to external link not working

Related Related

  1. 1

    Variable from function into window.location.href 'not defined'

  2. 2

    onclick window.location.href with input value

  3. 3

    String is not a function on window location href

  4. 4

    jQuery preventDefault() stops onclick from working

  5. 5

    Javascript window.location.href onClick not working

  6. 6

    window.location.href not working in IE 11

  7. 7

    Enable and disable window.location.href

  8. 8

    Redirect a Website to a local folder Using window.location.href() not working

  9. 9

    window.location = '' is not working ;

  10. 10

    Event.preventDefault stops php script from submitting correct values

  11. 11

    $window.location.href NOT WORKING in AngularJS

  12. 12

    window.location.href not working

  13. 13

    window.location.href and location.reload() does not load updated page from Server

  14. 14

    Make window.location.href load page from Server instead from browser cache

  15. 15

    JavaScript window.location.href not working in localhost

  16. 16

    window.location.href not working, mod rewrite or jquery?

  17. 17

    window.location.href not working on IE

  18. 18

    window location not working javascript

  19. 19

    location.href not working

  20. 20

    window.location.href and hash are not working on chrome

  21. 21

    Javascript window.location.href onClick not working

  22. 22

    window.location.href not working after Response.End()

  23. 23

    window.location.href not working as expected

  24. 24

    Make window.location.href load page from Server instead from browser cache

  25. 25

    window.location.href isn't working

  26. 26

    `window.location.href` not working as intended

  27. 27

    Any other ways apart from Window.location.href?

  28. 28

    *ngIf window.location.href.indexOf is not working

  29. 29

    angular4 unit test for window.location.href redirect to external link not working

HotTag

Archive