views:

33

answers:

1

Hey Guys,

Am having an issue with Safari. I have a basic script using jQuery that makes some changes to a form field and adds some behaviours to it. When I run this script on using the document ready method it's fine in Firefox but in Safari it doesn't always run the initial changes. I can hit refresh and get it running fine 4/5 times, but sometimes it just doesn't initialise like it should.

This is running on a local server so the refresh is pretty quick, I'm wondering if the javascript is executing before the page has finished loading. I've tried calling the script in the foot of the page rather then the header but that hasn't helped. I remember hearing about different browsers firing document ready at different times and thought this would help remedy it but it hasn't and I can't find any further information on that topic.

Anything I'm missing that could be the issue or a workaround? The script itself doesn't seem to be at fault. Am using the jQuery colours plugin, apart from that it's only jQuery and my script.

Help is always appreciated, thanks people!

Here is the code. initSearchBox() is run using the line below in the header.

$(document).ready(function() { initSearchBox() ; });

function randomFieldValue(){

var options = new Array(
    'Lorem',
    'Ipsum',
    'Dolor',
    'Sit',
    'Amet'
) 

t = Math.floor(Math.random()*(options.length - 1));

return options[t] ;

}

function initSearchBox(){

instanceDefText = randomFieldValue() ;

$('#search-form-field')
    .attr('value',instanceDefText)
    .css('color','#fff')
    .animate({color:'#999'},1500)
    .focus(function(){
        if($(this).attr('value') == instanceDefText){
            $(this).attr('value','')
                .css('color','#000')
        }
    })
    .blur(function(){
        if($(this).attr('value') == ''){
            instanceDefText = randomFieldValue() ;
            $(this).attr('value',instanceDefText)
                .css('color','#fff')
                .animate({color:'#999'},1500)
        }
    });

}

A: 

Have you looked at jQuery document ready bug in Safari / WebKit?

Saul