JavaScript window.onload not running on page refresh window onload run function,window onload run script,window.onload

I just started using RequireJs. Only one problem. Can someone explain why the code inside the require block is not run on page refresh. It only runs when I clear the cache.

example:

require(['game','comm','misc','soundutil','sprite','gui','constants'],function(Game,COMM,MISC,SoundUtil,Sprite, Gui,CNT){

    window.onload = function () {
        var canvas = document.getElementById('gameCanvas'),
         game = document.getElementById('game'),
         g = null,su = null;

        canvas.width = document.body.clientWidth;
        canvas.height = document.body.clientHeight;
console.log('Gamestate at beginning',CNT.GameState._current);

thanks

Answer:1

It does not look like your code is complete in your question but I'm going to hypothesize the following. What you describe looks like a race condition:

  • When the cache does not contain your data, the assignment to window.onload happens before the onload even is fired, so your handler is called.

  • When the cache already contains your data, then the onload event is fired before the assignment to window.onload happens, so your handler is never called.

I would suggest using the domReady plugin because it is designed to handle both cases. I've never used it myself but from what I can gather from the documentation, something like:

require(['domReady!', 'game','comm','misc','soundutil','sprite','gui','constants'],...

should work. The exclamation mark is not a typo, by the way. You then need to make your onload handler be the body of the function you pass to require.

Note that document.onload suffers from the same problem. It is even more prone to the problem you're having because it often fires before window.onload does.

ETA: As Shrike says, you can also use the jQuery method of waiting: $(document).ready(...). My rule of thumb here is if I'm already using jQuery (which I actually do in all my current projects), then I'd use $(document).ready(...), otherwise I'd use domReady. The difference between the two methods has been examined in detail in this question.

Answer:2

It's because at the moment of your code execution window is already loaded. So you're subscribing on an event which never fires. I'm not sure what kind if cache did you mean and why window's load fires.

You can easy fix your code if you use jQuery:

require(['game', 'comm', 'misc', 'soundutil', 'sprite', 'gui', 'constants'], function(Game, COMM, MISC,SoundUtil, Sprite, Gui,CNT){

  $(document).ready(function () {
  });
});
Answer:3

It looks pretty easy but I'm unable to do it. I want to change the view after click on login button, this is my code: app.js Ext.require ('Ext.container.Viewport'); Ext.application({ name: 'MyApp',...

It looks pretty easy but I'm unable to do it. I want to change the view after click on login button, this is my code: app.js Ext.require ('Ext.container.Viewport'); Ext.application({ name: 'MyApp',...

  1. switch view in android
  2. switch view in outlook
  3. switch view in swift
  4. switch view in servicenow
  5. switch view in android example
  6. switch view in angularjs
  7. switch view in skyrim
  8. switch view in iphone
  9. switch in view mvc

I have an array which I want to order by date in Angular.js: <tr ng-repeat="er in vm.readerFeeds | orderBy:'-publishedDate'"> <td>{{er.publishedDate}}</td> </tr> The list ...

I have an array which I want to order by date in Angular.js: <tr ng-repeat="er in vm.readerFeeds | orderBy:'-publishedDate'"> <td>{{er.publishedDate}}</td> </tr> The list ...

  1. angular order by date
  2. angular orderby date pipe
  3. angular order by date desc
  4. angular orderby date not working
  5. angular orderby date string
  6. angular orderby date descending
  7. angularjs orderby date
  8. angularjs orderby date string
  9. angularjs orderby date not working
  10. angularjs orderby date desc
  11. angularjs orderby datetime
  12. angularjs orderby date format
  13. angular order by datetime
  14. angularjs orderby date and time
  15. angular 2 orderby date example
  16. angular 6 orderby date
  17. angular 2 orderby date
  18. angular ngfor orderby date
  19. angular 2 orderby date pipe
  20. angular 6 orderby date pipe

I'm using this expression: "g{field1}={field2}a".match(/{([^\{\}]+)\}/g) What I need is to get "field1" and "field2", but it returns an array with "{field1}" and "{field2}".

I'm using this expression: "g{field1}={field2}a".match(/{([^\{\}]+)\}/g) What I need is to get "field1" and "field2", but it returns an array with "{field1}" and "{field2}".

  1. regular expression match any character
  2. regular expression match string
  3. regular expression match python
  4. regular expression match word
  5. regular expression match number
  6. regular expression match exact string
  7. regular expression match anything
  8. regular expression match multiple words
  9. regular expression match space
  10. regular expression match whitespace
  11. regular expression match period
  12. regular expression match newline
  13. regular expression match substring
  14. regular expression match javascript
  15. regular expression match parentheses
  16. regular expression match end of string
  17. regular expression match special characters
  18. regular expression match all characters
  19. regular expression match group
  20. regular expression match character

Firstly, I have checked these out: How can I get a Google Visulization LineChart to display vertical gridlines? Google Chart: AreaChart vAxis Gridlines is Not Drawing And they don't seem to be quite ...

Firstly, I have checked these out: How can I get a Google Visulization LineChart to display vertical gridlines? Google Chart: AreaChart vAxis Gridlines is Not Drawing And they don't seem to be quite ...