Search code examples
javascriptjquerydomjoomlamootools

What are alternatives to window.addEvent('domready',function() for window.location.href?


We are passing the url page to our Admin Emails through the 'refer' id in Joomla using ...

window.addEvent('domready', function() 
{ 
  document.getElementById('refer').value=window.location.href; 
});

This code works on some sites but not others. I have tried a jquery version with little luck, though I am open for suggestions. A site not returning the url via email is at http://www.freestylelitemeter.com while a site that is working is at http://www.comparediabetictestingsupplies.com. We are using 'refer' as a hidden field and everything matches, so I believe the issues is with the window.addEvent('domready', function() unless there is a conflict I am not aware of. Another interesting thing is the working domain has many more script files that has in the past found conflict between script files, while the smaller site has not.


Solution

  • At http://www.freestylelitemeter.com/, MooTools fails to load. You get this error in your JavaScript console:

    SCRIPT438: Object doesn't support property or method 'getElement'
    mootools.js, line 53 character 97

    MooTools extends window with addEvent(). Since MooTools hasn't loaded, addEvent() fails and your code is never executed.

    You have a buggy version of MooTools, correct that and your problem should be resolved.

    But, better yet, you are already using jQuery. Just change your code to this:

    $(function ()
    {
        $('#refer').val(location.href);
    });
    

    Are you even using MooTools much? You may be able to switch to jQuery exclusively, and load one fewer library.