Robert Kusznier
Robert Kusznier

Reputation: 6911

Passing value from HTML to JavaScript before the document is ready

I have an external JavaScript function that I need to call in each of my HTML files before the page is rendered (it's setting the locale), each time with a different argument. The script file with the function is included in the <head> element.

What are the way to pass an argument inside the script before the page is rendered? I'm unable to use hidden <input> or <div> then and would like to avoid embedded <script> calls.

Upvotes: 0

Views: 48

Answers (1)

theftprevention
theftprevention

Reputation: 5213

You can just introduce a locale variable in the global scope, within the <head> section, just before your script is called, and then modify your script to look for that variable.

<head>
    <!-- ... -->

    <script type="text/javascript">
        var locale = "fr-CA";
    </script>
    <script src="/js/myscript.js" type="text/javascript"></script>

    <!-- ... -->
</head>

Upvotes: 2

Related Questions