Jump to content

Recommended Posts

Hi, I am trying to build an embeddable widget, so that a user can embed a short piece of javascript on their site and it will track where users click.

 

My code for the HTML page is:

<script src="http://www.google.com/jsapi" type="text/javascript"></script>
<script type="text/javascript">
        google.load("jquery", "1.3");
</script>
<script src="clickmap.js" type="text/javascript"></script>
<script type="text/javascript">$(function() { $(document).saveClicks(); });</script>

I am trying to shorten it down so I can load the jquery and clickmap.js through one file like this:

 <script type="text/javascript" src="initial.js"></script>

The clickmap.js code is:

(function($) {
alert('HELLO');
$.fn.saveClicks = function() {
    jQuery(this).bind('mousedown.clickmap', function(evt) {
        jQuery.post('clickmap.php', {  
            x:evt.pageX,  
            y:evt.pageY,  
            l:escape(document.location.pathname)
        });
    });
};

         
})(jQuery);

I am able to load clickmap.js as the alert works but everything after that does not. I believe it may have something to do with loading the jQuery through google.load. When I load the jQuery through google.load without using inital.js the code will execute as it should, but when I load it like this:

<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>

Then the code will not execute.

 

Code for initial.js:

(function () {

    var scriptName = "initial.js"; //name of this script, used to get reference to own tag
    var jQuery; //noconflict reference to jquery
    var jqueryPath = "http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js";
    var jqueryVersion = "1.8.3";
    var scriptTag; //reference to the html script tag

    var allScripts = document.getElementsByTagName('script');
    var targetScripts = [];
    for (var i in allScripts) {
        var name = allScripts[i].src
        if(name && name.indexOf(scriptName) > 0)
            targetScripts.push(allScripts[i]);
    }

    scriptTag = targetScripts[targetScripts.length - 1];

    function loadScript(src, onLoad) {
        var script_tag = document.createElement('script');
        script_tag.setAttribute("type", "text/javascript");
        script_tag.setAttribute("src", src);

        if (script_tag.readyState) {
            script_tag.onreadystatechange = function () {
                if (this.readyState == 'complete' || this.readyState == 'loaded') {
                    onLoad();
                }
            };
        } else {
            script_tag.onload = onLoad;
        }
        (document.getElementsByTagName("head")[0] || document.documentElement).appendChild(script_tag);
    }

    if (window.jQuery === undefined || window.jQuery.fn.jquery !== jqueryVersion) {
        loadScript(jqueryPath, initjQuery);
    } else {
        initjQuery();
    }

    function initjQuery() {
        jQuery = window.jQuery.noConflict(true);
        main();
    }

    function main() {
        jQuery(document).ready(function ($) {
            loadScript("clickmap.js", function() { /* loaded */ });
        });
        
    }

})();

Could anyone help me fix this please?

Link to comment
https://forums.phpfreaks.com/topic/282414-loading-jquery/
Share on other sites

  • 4 years later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.