3

I'm using a third party script (Adobe DTM, useful to tag management purposes) on my pages, and it is causing the following problem:

I noticed that my pages are being reloaded right after being fully loaded for the first time (in the next load, it doesn't occur anymore), and the blame is on this function, present at one of the scripts loaded by the DTM tag in my HTML.

function Ne(Oe, Gc, nc, Ic) {
        if (Oe.targetJSLoaded) {
            return;
        }
        Ic.setCookie(Gc, true, nc);
        window.location.reload(); //in the first page load, this line is always executed
    }

It's pretty hard to figure out what is going on the script, once it has more than 5000 lines and it's obfuscated.

I need to prevent that window.location.reload() from being executed, but i have the following problems:

  1. Once is a third party loaded script (the script tag points to an URL which loads a bunch of other scripts during page load), i can't edit it manually.
  2. I can't break the user from reloading the page manually via F5.

I've tried something with the following jQuery code, but it doesn't work:

$(window).bind("beforeunload",function(e){
        e.preventDefault();
});

Is there a form to prevent that programatic reload, considering the situations described?

0

3 Answers 3

2

The key is Oe.targetJSLoaded. Find out what Oe is. Try following the call stack starting from the Ne function.

For example, the code from Harbor Freight is similar to yours:

TNT.a.ge = function (he, ie, Tb, nc, Ub) {
    if (!ie.targetJSLoaded) {
        Ub.setCookie(Tb, true, nc);
        he.location.reload();
    }
};

In your function and Harbor Freight's function, we want targetJSLoaded to be true.

Looking around, we find that TNT.a.ge is called by TNT.a.je, which is called by an anonymous function like so:

TNT.a.je(window, document, b, H, Ub);

In TNT.a.je, we deduce that ie.targetJSLoaded refers to window._AT.targetJSLoaded.

Define properties to override window._AT.targetJSLoaded to be true:

function overrideTargetJsLoaded(_AT) {
    Object.defineProperty(_AT, "targetJSLoaded", {
        value: true,
        enumerable: true,
        configurable: true
    });
    return _AT;
}

function override_AT() {
    let _AT = overrideTargetJsLoaded({});
    Object.defineProperty(window, "_AT", {
        get: function get_AT() {
            return _AT;
        },
        set: function set_AT(object) {
            _AT = overrideTargetJsLoaded(object);
        },
        enumerable: true,
        configurable: true
    });
}

Now, you need to override the targetJSLoaded before any other script is executed, so you put the script, which calls the override_AT function, before all other scripts on the webpage.

Sign up to request clarification or add additional context in comments.

Comments

2

Unfortunately your only solution is to modify the script and then replace the DTM tag so that it points to your own version (likely breaking it in the process), or simply stop using Adobe DTM. Any tracking that requires the page to be reloaded to work isn't very well designed.

Comments

-2

As long as you don't need to programmatically reload the page anywhere else, you could try overwriting the function.

window.location.reload = function(){}

5 Comments

Thanks Jake!! A question: Can this cause problems on form submits?
I don't think so, unless your submission callback calls location.reload().
I've tried it, and faced another problem. The dynamically loaded scripts are being loaded before my custom js. In other words, when i override window.location.reload, the page has already been reloaded. Damn!
Shoot. It looks like it's tricky override it in Firefox and Chrome. stackoverflow.com/questions/5204615/…
Nice and thanks for the alternate solution, but i'll fall in the same problem. All my custom scripts aren't going to make any effect since the DTM ones are being charged before all.

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.