If the page has an named anchor eg: <a name="section2"> (to support a link of mypage.html#section2 )then I think this will fail as link.href will be undefined.