0

I'm trying to write a javascript bookmark that, based on the current URL, navigates you to another page. Then after that navigation is done, it looks for a particular ID tag then navigates you again.

I have the first navigation done and working:

javascript:

(function() {
    ADCURL = "PageA.aspx";
    var temp = window.location.href;
    var tarr = temp.split("/");
    var serv = "https://" + tarr[2];
    var x = document.getElementsByTagName("html")[0].dir;
    if (x) {
        var x = document.getElementsByTagName("link");
        for (i = 0; i < x.length; i++) {
            var y = x[i].attributes[2].value;
            var lookingfor = "_vti_bin";
            var lookingforRegExp = new RegExp(lookingfor);
            if (lookingforRegExp.test(y)) {
                var z = y.replace("_vti_bin/spsdisco.aspx", ADCURL);
                var link = serv + z;
                window.location.href = link;
            }
        }
    } else {
        DL5 = document.links;
        for (lKi = 0; lKi < DL5.length; lKi++) {
            map = DL5[lKi];
            var lookingfor = "sites";
            var lookingforRegExp = new RegExp(lookingfor);
            if (lookingforRegExp.test(map)) {
                var lookingfor = "_layout";
                var lookingforRegExp = new RegExp(lookingfor);
                if (lookingforRegExp.test(map)) {} else {
                    var lookingfor = "SharedDocuments";
                    var lookingforRegExp = new RegExp(lookingfor);
                    if (lookingforRegExp.test(map)) {
                        var n = String(map).indexOf("SharedDocuments");
                        var x = String(map).slice(0, n);
                        var link = x + ADCURL;
                        window.location.href = link;
                    }
                }
            }
        }
    }
})();

however when I try to use a SetTimeout to delay the script so I can grab the ID i need from the page I navigated to, nothing happens (for now I just have a window alert in there as a placeholder until I get the SetTimeout to work):

javascript:

(function() {
    ADCURL = "PageA.aspx";
    var temp = window.location.href;
    var tarr = temp.split("/");
    var serv = "https://" + tarr[2];
    var x = document.getElementsByTagName("html")[0].dir;
    if (x) {
        var x = document.getElementsByTagName("link");
        for (i = 0; i < x.length; i++) {
            var y = x[i].attributes[2].value;
            var lookingfor = "_vti_bin";
            var lookingforRegExp = new RegExp(lookingfor);
            if (lookingforRegExp.test(y)) {
                var z = y.replace("_vti_bin/spsdisco.aspx", ADCURL);
                var link = serv + z;
                window.location.href = link;
            }
        }
    } else {
        DL5 = document.links;
        for (lKi = 0; lKi < DL5.length; lKi++) {
            map = DL5[lKi];
            var lookingfor = "sites";
            var lookingforRegExp = new RegExp(lookingfor);
            if (lookingforRegExp.test(map)) {
                var lookingfor = "_layout";
                var lookingforRegExp = new RegExp(lookingfor);
                if (lookingforRegExp.test(map)) {} else {
                    var lookingfor = "SharedDocuments";
                    var lookingforRegExp = new RegExp(lookingfor);
                    if (lookingforRegExp.test(map)) {
                        var n = String(map).indexOf("SharedDocuments");
                        var x = String(map).slice(0, n);
                        var link = x + ADCURL;
                        window.location.href = link;
                    }
                }
            }
        }
    }
setTimeout(function(){window.alert("yes");}, 2000);
})();

Any ideas what's going on? When using javascript in bookmarks is it not possible to do what I'm trying to do?

  • 2
    (1) Where is the call to `setTimeout`, (2) [mcve]. – DavidS Jun 13 '16 at 18:21
  • I don't quite understand the code you've written, but from your problem description I think your approach is wrong. [HTTP is stateless](http://stackoverflow.com/questions/29986657/global-variable-usage-on-page-reload), so after you've navigated to a new page, your current Javascript is no longer running. – DavidS Jun 13 '16 at 18:25
  • sorry - my first time posting on this site and I accepted someones edits to my code, but their edits removed the call to SetTimeout. – Anthony G. Jun 13 '16 at 18:26
  • No worries. Thanks for fixing it promptly. – DavidS Jun 13 '16 at 18:27

0 Answers0