1- why there are html code if the browser will redirect it automatically
It's traditional from the times where 302 was new and not every client understood what it means. It is not really needed today and uselessly wastes bandwidth.
2- if inject code in href, a javascript code can this executed before the redirection happened ? or not ?
If you have a proper 302 response, that is a Location header is present, the body will be ignored and nothing will be executed.
If no Location header is present (which would be an invalid response) it depends on the browser and context: if the URL with the bad 302 was included with img tag or was linked HTML it will often be rendered including the execution of Javascript inside the HTML. In other cases like downloads (link not leading to HTML) or script context it will instead result in an error.
There are firewalls which can be bypassed this way because they don't expect the browser to execute the body of 302 responses and thus skip inspection even if no Location header in the response indicates the target of the redirection.