2

I keep getting the following error Error: "Window navigated away" and I dont know how to stop this error from occuring when it does happen it also keeps me from leaving the current web page that I'm on. I'm fairly new to Jquery and Javascript so I kind of need lots of help on how to stop this from occuring.

Here is my code

if($('#button').length){
    paypal.Buttons({
        env: '<?= PAYPAL_ENVIRONMENT ?>',
        style:{
            height: 100,
        },
        createOrder: function() {
            let formData = new FormData();
            formData.append('return_url',  '<?= $baseUrl.$URL["redirectUrls"]["returnUrl"]?>' + '?commit=false');
            formData.append('cancel_url', '<?= $baseUrl.$URL["redirectUrls"]["cancelUrl"]?>');

            return fetch('<?= $rootPath.$URL['services']['orderCreate']?>',
                {
                    method: 'POST',
                    body: formData
                }
            ).then(function(response) {
                return response.json();
            }).then(function(resJson) {
                var txt = resJson.data.id;
                $.post('createid.php', {
                    order_id: txt
                });
                return resJson.data.id;
            });
        },
        onApprove: function(data, actions) {
            return fetch(
                '<?= $rootPath.$URL['services']['orderGet'] ?>',
                {
                    method: 'GET'
                }
            ).then(function(res) {
                return res.json();
            }).then(function(res) {
                $('.nonce').val(res.data.id);
            });

        },
        onCancel: function(data) {
            $('#c').html('<a id="anchor" name="anchor"><p>Cancelled  order.</p></a>');
            location.href = '#cancelled';
        },
        onError: function(err) {            
            $('#e').html('<a id="anchor" name="anchor"><p>Error.</p></a>');
            location.href = '#error';
        }
    }).render('#button');
}

1 Answers1

0

I got same error message and it was coming only in Microsoft Edge and IE and in my case it was due to extra space character in java-script code / jquery / php code below -

I changed ( Space is before "base" word )

location.href = "<?= base_url(); ?>Property/Transaction/error";

To

location.href = "<?=base_url(); ?>Property/Transaction/error";

If rollback that change again this problem comes and implementing this small change was solving it. so i thought to share it. you should also check for any special character in url that may also cause this kind of error in redirection.

How It came to my mind was due to some another question on stackoverflow - window.location.href not working on IE

Heemanshu Bhalla
  • 3,603
  • 1
  • 27
  • 53