0

I have a bookmarklet which a user adds to their own browser bookmarks toolbar which collects images from a page they are looking at.

I want to log each time a user is clicking on any site and store the data in a mysql table. So i'm using an ajax call to post to a php file which processes the data sent to it.

However, this sometimes works and sometimes does not. Meaning, it works on some sites and not others.

What I'm trying is this:

(function()
{ // declare variables e.g. div.ids, div content etc then display it
......

    //log the click
    var dataString = '&url=' + encodeURIComponent(window.location.href) + '&page_title=' + encodeURIComponent(document.title);
    $.ajax({
    type: "POST", // form method
    url: "http://myurl.com/includes/log_clicks.php",// destination
    data: dataString,
    cache: false
    });
    //END log the click

})();

When it doesn't work and i use Firebug to find out why, i sometimes get the error: TypeError: $ is undefined $.ajax({

Sometimes it still posts to the php file but with no data

Is there a better way to call ajax from within a js file on a user's browser?

As per suggestions, I've tried loading jquery by simply amending one of the variables like so:

div.innerHTML = '<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script><div class=....';

But that made no difference

1
  • The page has to have jQuery loaded, or you have to load it yourself. Commented Dec 2, 2012 at 2:05

1 Answer 1

1

You need jQuery present on the page in order to perform this. You will need to load jQuery if not present. A great approach is outlined here using the jQuerify code which actually just loads a portion of jQuery functionality that is needed.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for this, i tried jQuerify on a site causing me problems, Amazon, and it tells me jquery is already loaded... albeit an older version 1.2.6 but I believe ajax should still work with that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.