document ready and window load not always working


I have a code that executes some lines of code on document ready or window load. My problem is that sometimes the code works and sometimes not. I see that if i press the refresh button on the browser, always work well. But when I enter to the page from other page using href, sometimes work and sometimes not. I try with cache disabled, link follow by parameter, use full url, but nothing works.

When works, works well, but there is sometimes that doesn't work. I notice that when the network is a little slow, the problem is frequent. But with the refresh always work, any idea?

My page works with jquery and angular js. The thing that most intrigue me is why sometimes work and sometimes don't!

When I enclose the document ready in a setTimeout function and gives a time of 1 or 2 seconds then works
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
Can you post a link or some source code - not much we can do on the description above.
greetings joyacv2, , I have seen something like this happen, when the web page developer has so very much Javascript loading in the page (librarys  like jquery, angular js, bootstrap, prototype js, etc) And then has many, many things that initialize and start up in the page load sequences (window load) . There are so many javascript things that are trying to establish their Objects and set all of their page elements functioning, that the browser can get confused and mixed up because of the overflow of javascript trying to happen at start up. . .
 The main reason I think this is because you say -
       "use setTimeout function and gives a time of 1 or 2 seconds then works "

You may should try and reduce the javascript, , that normally the demo and example code places in the start up page (document ready), and have a setTimeout function in the document ready, of about a second (800 ms) and place some or more of the extra initialization codes in that setTimeout, to allow the librarys and base javascritp to get established first. You may can reduce some of the javascript that you are loading?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
joyacv2Author Commented:
Thank You very much! Your answer confirm my analysis!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.