Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6141901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:18:53+00:00 2026-05-23T18:18:53+00:00

I have a javascript variable in the head of my dom and I need

  • 0

I have a javascript variable in the head of my dom and I need to access it in an external js file but it seems to come up undefined.

In my head I have something like.

 <head>
<script type="text/javascript">
    var overlayAlignment = ["left:20px","right:40px","left:50px"];
</script>
    </head>

external .js file

$(document).ready(function () {
alert(overlayAlignment[0]);
});

In my external js file I want to use the variable but something like this comes up undefined all the time, any idea what I am doing wrong.

I found that I could run a function from the external .js in the head to set the overlayAlignment variable if I add the overlayAlignment variable to the .js file.

 <head>
<script type="text/javascript">
    setOverlayAlignment("value1", "value2", "values3");
</script>
</head>

external .js file

function setOverlayAlignment(value1, value2, value3) {
    overlayalignment[0] = value1;
    overlayalignment[1] = value2;
    overlayalignment[2] = value3;
}

This still comes up as undefined when I try to use it in my jquery function however. Strange, I thought $(document).ready was to tell the function to wait till the dom is loaded to run, if that is the case why is overlayAlignment undefined when it runs?

I need to do it like this because the overlayAlignment variable values are only known at runtime.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T18:18:54+00:00Added an answer on May 23, 2026 at 6:18 pm

    Weird just tried this and it worked. Strange I thought window.onload and $(document).ready were the same thing.

    window.onload = function () {
        $(document).ready(function () {
            alert(overlayAlignment[0]);
        });
    }
    

    Apparently overlayAlignment was not ready when $(document).ready was called but it will be available after window.onload.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My website dynamically embeds an external Javascript file into the head tag. The external
I have declared branch as a variable in the head. Its a JavaScript function
I have a JavaScript variable: var s = <html><head><style>body{a:b;c:d}</style></head><body></body></html>; First, I am extracting the
i have code like this: loading js files dynamically via another js file? but
I have this basic auto complete JavaScript that works well, but you need to
I have javascript string variable with var sttr=We prefer questions that can be answered
I have a paragraph of text in a javascript variable called 'input_content' and that
I have a long snippet of HTML I want some javascript variable to equal
I have a select control, and in a javascript variable I have a text
Can you setup a variable for javascript in one page and have another page

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.