Should I use window.navigate or [removed] in JavaScript?

前端 未结 11 1679
小鲜肉
小鲜肉 2020-11-27 12:12

What\'s the preferred method to use to change the location of the current web page using JavaScript? I\'ve seen both window.navigate and document.location used. Are there an

11条回答
  •  北海茫月
    2020-11-27 13:15

    Late joining this conversation to shed light on a mildly interesting factoid for web-facing, analytics-aware websites. Passing the mic over to Michael Papworth:

    https://github.com/michaelpapworth/jQuery.navigate

    "When using website analytics, window.location is not sufficient due to the referer not being passed on the request. The plugin resolves this and allows for both aliased and parametrised URLs."

    If one examines the code what it does is this:

       var methods = {
                'goTo': function (url) {
                    // instead of using window.location to navigate away
                    // we use an ephimeral link to click on and thus ensure
                    // the referer (current url) is always passed on to the request
                    $('').attr("href", url)[0].click();
                },
                ...
       };
    

    Neato!

提交回复
热议问题