What's the difference between a location.href jump and a click event set to the a tag?

I originally used window.location.href=url to jump to relevant pages, but in some third-party app, I found that it didn"t seem to work. Looking at other people"s code, he first created a hidden a tag, and then gave the a tag a click event to perform the jump:

var a = document.createElement("a");

a.setAttribute("href", aV);
a.style.display = "none";

var ev = document.createEvent("HTMLEvents");

ev.initEvent("click", false, true);
a.dispatchEvent(ev);

so what"s the difference between a location.href jump and setting a click event for a tag?

Mar.07,2021

Wherever possible, you should use over window.location.href , for a number of very good reasons.

If you have javascript disabled, none of the links would work.
Sp
iders, such as Google Bot, do not interpret javascript, and so they won't follow any of your links.
IT BREAKS THE INTERNET. No, really though-the World Wide Web is built on the very basis of discoverable linkages between pages. Hiding these linkages with non-standard.. Err, links, goes against that very premise.
It makes for a bad user experience: a user expects that when they mouse over a link, they will have access to some information:
the destination displayed in the status bar (very important!)
right-click-> copy link location
middle-click-> open new tab
etc
Using window.location breaks all of these
It's much worker!

Reference: https://stackoverflow.com/que.

Menu