0

I am trying to auto refresh page but IE showing the following error. I am using IE version 11.

Error - Unable to get property 'preventDefault' of undefined or null reference

My code is -

$(function (e)
    {
        setTimeout(startRefresh_Test, 8000);
    });
    function startRefresh_Test(e)
    {
        $.get('', function (data)
        {
            $(document.body).html(data);
        });
        Get_event(e); 
    };

    function Get_event(e) {
        var evt = e || window.event; 
        if (evt.preventDefault) {                
            evt.preventDefault();
        } else {            
            evt.returnValue = false;
            evt.cancelBubble = true;
        }            
    };

Please, anyone help me how to solve this problem.

Thanks

Update:

I am using JQuery $.get method other problme is using different method.

$(function (e)
    {
        setTimeout(startRefresh_Test(e), 8000);
    });
APC
  • 139
  • 3
  • 13
  • Possible duplicate of [event.preventDefault() function not working in IE](http://stackoverflow.com/questions/1000597/event-preventdefault-function-not-working-in-ie) – JonH Sep 28 '16 at 14:37
  • Your variable `evt` is either null or undefined. Function `setTimeout` does not send `event` variable to your defined `startRefresh_Test` callback – Domas Mar Sep 28 '16 at 14:37
  • @DomasMar, I have changed setTimeout(startRefresh_Test, 8000); to setTimeout(startRefresh_Test(e), 8000); and it is doing nothing. – APC Sep 28 '16 at 14:41
  • Notice, that the passed `e` in `$(function (e) {...})` is `jQuery` function, not an event object. In your code that is shadowed by `e` in the argument list of `startRefresh`, though. In that function `e` is `undefined`. – Teemu Sep 28 '16 at 14:42
  • @Teemu, how to defined that, could you please give me an example – APC Sep 28 '16 at 14:46
  • What is the actual event object you want to grab? `onload`? `onunload`? Some else? Also note that `$.get` is asynchronous, you're calling `Get_event(e);` before the callback will be executed. Furthermore, the global event object (IE and Chrome only) is alive only in actual event handlers. You can store a reference to it, but this has to be done in an event handler function. – Teemu Sep 28 '16 at 14:51
  • @Teemu, i want it onload – APC Sep 28 '16 at 14:52
  • The original or the one you might think `$(document.body).html(data);` would fire? (it doesn't though). Also, [`load`](https://developer.mozilla.org/en-US/docs/Web/Events/load) doesn't have any specific default action to prevent, nor it's cancelable (I'm not sure if jQuery makes it bubble, by default it doesn't). Can you please explain the idea behind your code, and why you need it? Maybe we could re-write it to do what you need. – Teemu Sep 28 '16 at 15:03
  • @Teemu, I am trying to refresh page and get data without page blink, also, no prompt on IE – APC Sep 28 '16 at 15:12
  • Hmm, that's beyond the onload event, see http://stackoverflow.com/a/12358419/ – Teemu Sep 28 '16 at 15:21
  • @Teemu, is there any way to force ie – APC Sep 28 '16 at 15:27
  • To prevent the white flash? Afaik no, it depends on so many factors, the HTML, images, size of the scripts and CSS etc.. Please read the SO answer I linked above. By searching for _white flash_ you'll find more information. There's no `prompt` in your code, can't say anything about it. – Teemu Sep 28 '16 at 15:33
  • @Teemu, is not possible to use prevent default in IE, I dont mind flashing in IE but no prompt – APC Sep 28 '16 at 15:35
  • Like I said, there's no `prompt` in the example code ... Please ask a new question about "prompt", add the relevant code, and explain in details what you need in that _new post_. – Teemu Sep 28 '16 at 15:40

0 Answers0