[BUGFIX] Javascript for TMENU_LAYERS and GMENU_LAYERS missing

If using TMENU_LAYERS/GMENU_LAYERS and (for example) indexed_search on
the same page, the JavaScript header tags are missing if the page is
loaded from the cache.

Resolves: #36719
Releases: 4.5, 4.6, 4.7, 6.0, 6.1
Change-Id: Icd52f68d2b9462360f0b9efd912d93d4b0c1048c
Reviewed-on: https://review.typo3.org/19263
Reviewed-by: Anja Leichsenring
Tested-by: Anja Leichsenring
Reviewed-by: Christian Kuhn
Tested-by: Christian Kuhn