Avatar of trevor1940
trevor1940

asked on 

WordPress how to stimulate body onload

Hi

I wish to display an openlayers map within Wordpress

Usually the openlayers JavaScript is fired from
<body onload="init()" >

Open in new window

 

How do I stimulate this in WordPress?

So A [Shortcode] on my page will point to the PHP / html markup where I will enqueue the necessary scripts but how do I trigger the "init()" function?
WordPressJavaScriptPHP

Avatar of undefined
Last Comment
trevor1940
ASKER CERTIFIED SOLUTION
Avatar of James Bilous
James Bilous
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of trevor1940
trevor1940

ASKER

OK that would work
I Assume I would need to will enqueue JQUERY before my script?
Avatar of James Bilous
James Bilous
Flag of United States of America image

Thats correct
You add Custom Js code using following plugins.

https://wordpress.org/plugins/custom-css-js/
https://wordpress.org/plugins/css-javascript-toolbox/

You can add code given by James using above simple plugins
Avatar of trevor1940
trevor1940

ASKER

@James Thanx I've been scratching my head for days on this

If interested this is how I've achieved  a test map now on to the actual

PHP
<?php

/*
Plugin Name: Open Layers
Plugin URI: http://URI_Of_Page_Describing_Plugin_and_Updates
Description: A brief description of the Plugin.
Version: 1.0
Author: Trevor1940
Author URI: http://URI_Of_The_Plugin_Author
License: A "Slug" license name e.g. GPL2
*/
function load_ol_stuff(){
      wp_enqueue_script("jquery");
      
    wp_enqueue_script('openlayers',
        "//openlayers.org/api/OpenLayers.js","2.0",false);
    wp_enqueue_script('OSM',
        WP_PLUGIN_URL. "/OpenLayers/js/ol.js",
        array('openlayers'),'1.0',false);
} // end load stuff



// load scripts required for plugin

add_action('get_header',load_ol_stuff);

function get_map(){
    $html = <<<HTML
<div style="width:400px;height:400px;" id="draw-map-here"></div>
HTML;

    echo $html;


}

add_shortcode( 'get-ol-map', 'get_map' );

Open in new window



ol.js
 jQuery(function($){
  init();
 });
 
function init(){
//    alert("Hello word");
    map = new OpenLayers.Map('draw-map-here');
    originalOSM = new OpenLayers.Layer.OSM("OpenStreetMap");
    map.addLayers([originalOSM]);
    map.setCenter(new OpenLayers.LonLat(96.33899700000126,2.4700000000000246).transform(new OpenLayers.Projection("EPSG:4326"),map.getProjectionObject()), 10);
;}

Open in new window

Avatar of trevor1940
trevor1940

ASKER

Thank you
JavaScript
JavaScript

JavaScript is a dynamic, object-based language commonly used for client-side scripting in web browsers. Recently, server side JavaScript frameworks have also emerged. JavaScript runs on nearly every operating system and in almost every mainstream web browser.

127K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo