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 7833417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T12:42:03+00:00 2026-06-02T12:42:03+00:00

I am new to javascript and want to understand what is going on with

  • 0

I am new to javascript and want to understand what is going on with my code. I am using jQuery and OpenLayers to do some web mapping.

Here is code example 1, where my map variable is created within the onReady function:

$(document).ready(function(){
    var map = new.OpenLayers.Map('map', options);
    $.get(my_python.cgi_script which returns an html table of available layers)
    });

As far as I understand, my ‘map’ variable has a local scope.

Within that function I add some layers, generate controls, etc. All working and covered in the OpenLayers documentation. I am also using jQuery’s get method to call a python.cgi script which dynamically generates a table of available layers. This all occurs within the above onReady.

I needed to work with the dynamically generated content and found that I needed to place my code into an onLoad function. If I place the second codeblock into an onReady function the thumbnails are not accessible via jQuery because of the order in which things are rendered.

$(document).ready(function(){
    var map = new.OpenLayers.Map('map', options);
    //More code to dynamically generate a list of available layers, stored in a table
    $.get(my_python.cgi_script which returns an html table of available layers)
    });

$(body).onLoad(function(){
 $(img.thumbnail).bind('click', function(){
  var name = $(this).attr('layer_name_id')
  var layer = new OpenLayers.WMS(//all the stuff to add the layer, including name)
  map.addLayer('layer') //map undefined due to scope
});
});

In the above block ‘map’ is undefined in the second variable. I understand why (scope) and decided that map needed to be a global variable. I then tried:

var map; //This is a global because it is defined outside of any functions?
$(document).ready(function(){
    map = new.OpenLayers.Map('map', options);
    //More code to dynamically generate a list of available layers, stored in a table
    $.get(my_python.cgi_script which returns an html table of available layers)
    });

$(body).onLoad(function(){
 $(img.thumbnail).bind('click', function(){
  var name = $(this).attr('layer_name_id')
  var layer = new OpenLayers.WMS(//all the stuff to add the layer, including name)
  map.addLayer('layer')
});
});

The map variable is still undefined. Why? Am I misunderstanding how a global works?

I was able to get everything working by putting the onLoad function inside of the onReady code block. I am not sure why the below is working:

$(document).onReady(function(){
    var map = new.OpenLayers.Map('map', options);
    //More code to dynamically generate a list of available layers, stored in a table
    $.get(my_python.cgi_script which returns an html table of available layers)

$(body).onLoad(function(){
 $(img.thumbnail).bind('click', function(){
  var name = $(this).attr('layer_name_id')
  var layer = new OpenLayers.WMS(//all the stuff to add the layer, including name)
  map.addLayer('layer')
  });
});   

});

  • 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-06-02T12:42:04+00:00Added an answer on June 2, 2026 at 12:42 pm

    Code snippet where you declared global variable map is working. But Jquery has no method onReady and onLoad. Better create init() function that will be executed in $(document).ready() function dont mix $(document).ready and body.onload functions. Use init function and it document.ready event.

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

Sidebar

Related Questions

I m new to using Web services using JavaScript. All I want is to
Lately I've been writing some JS code using jQuery and JavaScript as it is
I'm brand spankin' new to Javascript. Here's what I want to do. I want
Sorry for the semi-rant here. I am hooked on javascript and want some direction
I am using Selenium WebDriver and want to execute some javascript from a webpage.
I am new to javascript.I want to move a car on path with arrows.I
If I want to try out something new in Ruby or Javascript, I love
I want it to have a popup dialog javascript box. Instead of a new
I am new in share point. I want to select data using java script
I'm using Nodejs and Socket.io. When the client connects, new JavaScript objects are created.

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.