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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T02:18:48+00:00 2026-05-19T02:18:48+00:00

My aim is to load an OpenLayers map using an external JS file. In

  • 0

My aim is to load an OpenLayers map using an external JS file. In the head I have a jQuery library, and in the body I have the OpenLayers library, my Openlayers code and the div tag.

<script type="text/javascript" src="http://example.com/OpenLayers.js"></script>
<script type="text/javascript" src="http://example.com/MyMapScript.js"></script>
<div id="map"></div>

However I don’t have access to the body tag so I can’t fire onload="init()" which is how most of the examples work.

Instead, at the bottom of MyMapScript.js I’ve added the line below.

jQuery(window).load(init());

This is where it gets a little odd. Using Firebug I can see that this code is called and that it initializes the map object with all it’s properties. I can also see that it is calling the WMS server and getting all the map tiles successfully. However it doesn’t actually draw anything on the page and no extra divs are added to the HTML.

This page talks about a similar issue but I’m not sure how to apply it to my situation as for me init() is actually being called.
http://bytes.com/topic/javascript/answers/855670-unusual-behavior-function-calls-java-script

The solution I’ve gone for in the end is to do this.

<script type="text/javascript" src="http://example.com/OpenLayers.js"></script>
<script type="text/javascript" src="http://example.com/MyMapScript.js"></script>
<script type="text/javascript"> window.onload=init; </script>
<div id="map"></div>

Which works but feels like a fudge.

Is there a way to initialize an OpenLayers map from an external javascript file?
Is there anything wrong with my window.onload=init; solution?
If I have to do it inline with the HTML is there are better way of doing it?

  • 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-19T02:18:49+00:00Added an answer on May 19, 2026 at 2:18 am

    I fixed the issue myself. My initial code worked just fine apart from two things. I was missing the width and height on the div tag, and I had ordered the statements so that the div came after the javascript files, rather than before.

    It should have looked like this.

    <div id="map" style="width:400px; height:400px;"></div>    
    <script type="text/javascript" src="http://example.com/OpenLayers.js"></script>
    <script type="text/javascript" src="http://example.com/MyMapScript.js"></script>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an example of some code that I see often in websites that
To put this in concise language... Aim: To create a class which can load
The aim is to show a form which will update the div on the
We have developed an application in c# .net and aim to make it deployable
I have been trying to organise my views to optimize load times by having
I am using RJS with Prototype and I am trying to replace HTML code
My Aim: Sending an email (e.g. support request, error report) out of a C#
Does anyone know of any existing projects that aim to port Android's Java VM
SVN and CVS are two very different systems that aim to address the same
I'm trying to find any JavaScript frameworks whose sole aim is to standardize the

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.