0

First, I have tried for several hours to weed through the various search results of the keywords above. I've found several things that seem to answer this but they're for Drupal 7; one of those was posted on 2012 and I am pretty sure things are gosh darn different now.

I am not a programmer, nor a web dev professional; I am just someone trying to update a very old site that worked fine but that needs to be re-freshen. I have some experience as a content manager with Drupal 6, but I have never got into coding, building modules, or editing template files. I am not afraid to copy a file, open it in an editor, change this or that, cut or paste, but I don't see myself being able to build a module to do this.

Maybe there's no way to do this "easily."

I want to replicate a Google map with a custom search feature and some other tweaks. It works nicely in the existing site and it is basically 3 parts: a <script>code here</script> in <head>, some HTML and script in the body, and the following markup for <body>.

<body onload="load(); curvycurvy()" onunload="GUnload()">

All I am hoping to do is properly add this code on my new Drupal 8 front page. I've used the Asset Injector module to add the bulk of the script to <head> and used <front> to restrict it to the front page. I have chosen PHP for the content and it seems to retain the script.

Is there a way to add this code above into the body? Failing that, is there a way to provide similar functionality?

I was hoping that the Asset Injector has some tools like, "Add to the Body Tag" or such, that would have been exactly what I need.

1
  • Are you using a custom theme on the website? Or a 3rd party theme? (Go to www.mysite.com/admin/appearance to check. If you are using a custom theme, it can be done fairly easily with a little bit of code.
    – 100pic
    Commented Dec 19, 2018 at 1:32

2 Answers 2

1

This is fairly easy and covered well in the Javascript API section of the drupal docs.

Effectively you want a Javascript file that creates a Drupal behavior (this is automatically fired when the document is ready). Inside the behavior, just call curvycurvy();.

https://www.drupal.org/docs/8/api/javascript-api/javascript-api-overview

So, for starters...

Drupal.behaviors.initCurvy = {
  attach: function (context, settings) {
    curvycurvy();
  }
};
0

It's not really considered good practice, but since you are already using the Asset Injector Module, you could inject some jQuery to replace the existing code written in the body tag:

/* This replaces "<body onload="load(); curvycurvy()" */
$(document).on('load', function(){
   load(); 
   curvycurvy();
});

/* This replaces onunload="GUnload()"> */
$(window).unload(function() {
  GUnload();
});

Not the answer you're looking for? Browse other questions tagged or ask your own question.