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

  • Home
  • SEARCH
  • 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 7563143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:35:22+00:00 2026-05-30T13:35:22+00:00

I have a page on which I’m using JQuery Mobile. I am having difficulty

  • 0

I have a page on which I’m using JQuery Mobile. I am having difficulty understanding when exactly my variables are visible. I thought that the following were loaded into the same memory space. But I get an error. Before I explain the error let me show the code:

Page.html

<html>
  <head>
    <script type="text/javascript" src="app.js"></script>
  </head>

  <body>
    <div id="myPage" data-role="page">
      <div data-role="header">
        <h1>My App</h1>
      </div>

      <div data-role="content">
        <div class="ui-grid-a">
          <div class="ui-block-a"><input type="button" value="First" onclick="return button1_Click();" /></div>
          <div class="ui-block-b"><input type="button" value="Second" onclick="return button2_Click();" /></div>
        </div>
      </div>

      <script type="text/javascript">
        var currentIndex = 0;
        function button1_Click() {
          alert(myCollection.length);
        }

        function button2_Click() {

        }
      </script>
    </div>
  </body>
</html>

App.js

$("#myPage").live("pagecreate", function () {
});

$("#myPage").live("pagebeforeshow", function () {
});

$("#myPage").live("pageshow", function () {
  var myCollection = loadCollection();
  function loadCollection() {
    // do stuff
  }
});

When I click the “First” button, my event handler is called. However, I receive the following error:

ReferenceError: myCollection is not defined

My question is how can I share variables between these two files? I’m also concerned about name collisions which is why I was hope to share the memory space in the manner I attempted below. What am I doing wrong?

Thank you.

  • 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-30T13:35:23+00:00Added an answer on May 30, 2026 at 1:35 pm

    myCollection is defined within the scope of a callback function and thus only exists within that callback function. It is not available outside that scope. If you want it available globally, you have to define it as a global variable, outside of a function scope.

    Your code in app.js also should be surrounded with $(document).ready() because you’re trying to reference the DOM before it exists.

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

Sidebar

Related Questions

I have Page A which calls Page B using AJAX. Page B will be
I have a page which tries to get the value from another server using
I have got page which I would like to cache using the OutputCache directive.
I have a page which includes a css reset. On that page, I have
I have a page which requires retrieving results from a service, and that takes
I have a page which opens another page using window.open and does some work
I have a page which uses jQuery for some AJAX-stuff und Django as main
I have a page which has a couple option links that set GET-based parameters
I have page which is redirected from htaccess. now I can pass the German
I have a page which spawns a popup browser window. I have a JavaScript

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.