0

I have the following problem: on a customer's homepage the navibar is loaded by javascript, but I need to change some URL's on it. If I just start my script on $(document).ready() it runs before the customers script and has no effect. I only can use setTimeout for my function to wait until the other script is ready, but it's not good or safe at all. I can't change anything on the website, only add a javascript - is there a way to time it after the other one?

6
  • What is the customer's script? Commented May 16, 2012 at 15:46
  • You can use a deferred done call using jQuery. Commented May 16, 2012 at 15:46
  • @MarcusRecck What deferred object would he apply the done method to? Commented May 16, 2012 at 15:47
  • Well he wants to time it so one calls after the other, at least that's what I've gotten from his text. Once the first one calls, he can resolve the deferred object and then call the done method to run the second one. Commented May 16, 2012 at 15:48
  • The problem is there is no way to resolve the deferred object, it would have to be resolved by customer's script which he has no access to. I don't think there is a way to do it other than using a setTimeout solution in this case. Commented May 16, 2012 at 15:51

4 Answers 4

1

You can use repeated setTimeout, in order to check if menu is accessible.

function check_menu(){
    if(document.getElementById('my_menu')==null){
        setTimeout('check_menu()',500);
    } else {
        //do some stuff
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

@iambriansreed Why would setInterval vs setTimeout warrant a downvote? I would actually suggest setTimeout over setInterval due to having more control over setTimeout. The only change I would suggest is using check_menu instead of "check_menu()"
setTimeout is used here because we want to call the method only 1 time, it's easier to stop when we want.
1

If you have information about the menu like the id or class, use the onLoad() jQuery method on the element. For example if the code is loading asynchronously, and you add the onload to one of the last elements it should fire after the content has finished.

$.post('AsyncCodeLoad.php', function(data) {
    $('#lastElementToLoad').onLoad(RunMyFunction);
});

Or if you have no chance to insert your code into the async loading just add to the bottom of the </body>:

    $('#lastElementToLoad').onLoad(RunMyFunction);

Just a thought.

Comments

1

Yes, add your script at the bottom of the <body /> tag to ensure it does not run until all other scripts have run. This will only work however if your customer is loading the nav links synchronously.

If the nav is being loaded asynchronously, use JS's setInterval to repeatedly check the contents of the nav for links. When you determine the links have been added, cancel your interval check and call your script's logic entry point.

Cheers

Comments

0

Fiddle: http://jsfiddle.net/iambriansreed/xSzjA/

JavaScript

var
menu_fix = function(){
    var menu = $('#menu');
    if(menu.length == 0) return;
    clearInterval(menu_fix_int);
    $('a', menu).text('Google Search').attr('href','http://google.com');        
},
menu_fix_int = setInterval(menu_fix, 100);

HTML

<div id="menu"><a href="http://bing.com">Bing Search</a></div>​

Comments

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.