What's the difference between [removed] and [removed] in JavaScript?

后端 未结 16 1969
故里飘歌
故里飘歌 2020-11-22 12:37

Should both of them reference the same object?

16条回答
  •  伪装坚强ぢ
    2020-11-22 13:25

    Despite of most people recommend here, that is how Google Analytics's dynamic protocol snipped looked like for ages (before they moved from ga.js to analytics.js recently):

    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    

    More info: https://developers.google.com/analytics/devguides/collection/gajs/

    In new version they used '//' so browser can automatically add protocol:

    '//www.google-analytics.com/analytics.js'
    

    So if Google prefers document.location to window.location when they need protocol in JS, I guess they have some reasons for that.

    OVERALL: I personally believe that document.location and window.location are the same, but if giant with biggest stats about usage of browsers like Google using document.location, I recommend to follow them.

提交回复
热议问题