How can I prevent Firefox from speculative load of a javascript file?

458 Views Asked by At

I have some javascript code in the HEAD that sets a cookie. Later at the bottom of the page I load another javascript file. This file is served by a dynamic back-end page that uses the cookie to determine what to return.

A problem occurs in Firefox because it uses speculative parsing: it loads the javascript file at the end of the page before the javascript in the HEAD has executed. Since the cookie has not yet been set when the second javascript file is loaded, the back-end page does not receive the cookie value.

Is there any way to prevent Firefox from loading the second javascript file too soon?

1

There are 1 best solutions below

1
On

If you want a script to be fetched over HTTP after another script has run (instead of when the browser finds the script in the DOM it is creating by parsing HTML) then generate the second script element using JS/DOM instead of putting it in the HTML.