1

I have a code which uses the jquery $(document).ready() method and is evaluating a click on an item.

Im a PC everything works fine, but the target group of my page are smartphone users. With a smartphone the behaviour of this script is erratic and I suspect that above named $(document).ready() is the culprit.

What can I do?

First option: Put my code back into the HTML code instead of loading a script file in the header of my page? So I can be sure the code is there? Possible but ugly and messy.

Second option: Substitute $(document).ready() with something else? If yes with what? I am not using jquery mobile yet and until now it wasn't necessary and I hesitate to implement it just to make one line work.

Has someone an idea how to deal with this?

Here is part of my code:

var track = function (myLink, func) {
    var redirectUrl = myLink;
        alert(redirectUrl);
    // Do things here....

    //Call ajax with parameters set before...
    $.ajax({
            type: "GET",
            url: sUrl, // Url to webservice
            data: clickdata, // Data to be send to webservice
            dataType: "text",
            timeout: 100,
            complete: func
        }
    );
}

// Show loading gif and track clicks on banners.
$( document).ready(function(){
    function loading_anchor(e) {
        e.preventDefault();
        var qa = $(this);

        function loading_bar() {
            window.location = redirectUrl;
            // Show loading bar everytime we load a after a click.
            // Not important code here....
        }

        var redirectUrl = e.currentTarget.href;
        if (qa.hasClass("previewlink") || qa.hasClass("productbanner")) {
            // In case of a click on a banner we want to track this click.
            track(this, loading_bar);
        }
        loading_bar();
    }

    $('a[loading="1"]').click(loading_anchor);
});

What should happen here ( and mind me happens perfectly on a pc) is tracking the click on certain items through calling a webservice. The webservice gives nothing back and I don't need anything from it. After the call the default behaviour is executed: forwarding to the link target and while still lingering on the present page I show a loading gif.

Calamity Jane
  • 2,189
  • 5
  • 36
  • 68
  • 4
    This amazing answer could he helpful http://stackoverflow.com/a/14469041/263989 – fasouto Jul 17 '14 at 11:49
  • We will need to know more about your page and its configuration. – Boaz Jul 17 '14 at 11:50
  • 1
    @fasouto OP is clearly stating the page does not use jQuery Mobile – Boaz Jul 17 '14 at 11:52
  • fasouto I have read this answer. It is for jquery mobile. So you would suggest to implment additionally jquery mobile? – Calamity Jane Jul 17 '14 at 11:52
  • @CalamityJane If you're loading content dynamically, `$(document).ready` might still be executing before your AJAX requests have been completed and the content rendered. If you're binding a `click` handler in the `ready` statement and it's not working, you should consider deferring the binding until after the requests have completed. You can also delegate the event handler instead of binding it. – Boaz Jul 17 '14 at 11:56
  • @Boaz My bad, you are right – fasouto Jul 17 '14 at 11:57
  • after a lot of tries, testing and discussion with my coworker I decided to (insert swearwords of your choice) move the whole logic into my php code. That means that I forward the parameters to my next controller (luckily every link results in one on my own page or it would not work) and evaluate the parameters there. – Calamity Jane Jul 17 '14 at 13:24

0 Answers0