How to add JavaScript for all the pages from the base.html?

How to add JavaScript for all the pages from the base.html?

What I did?
Created a JavaScript file ‘gamescript.html’ and the contents are below:

<script type="text/javascript">
	atOptions = {
		'key' : '983dqhf9ajfa763ha74hr6gafha7e',
		'format' : 'iframe',
		'height' : 360,
		'width' : 468,
		'params' : {}
	};
	document.write('<scr' + 'ipt type="text/javascript" src="http' + (location.protocol === 'https:' ? 's' : '') + '://www.gamedevenetwork.com/qhf9ajfa763ha74hr6ga/invoke.js"></scr' + 'ipt>');
</script>

And then I modified the base.html file like below. Change only line no 13 ie ‘{{ partial “gamescript.html” . }}’

<html class="nojs" lang="{{ site.LanguageCode | default site.Language.Lang }}" dir="{{ site.Language.LanguageDirection | default "ltr" }}">

<head>

<meta charset="utf-8">

<meta name="viewport" content="width=device-width">

<title>{{ block "title" . }}{{ .Title }} – {{ site.Title }}{{ end }}</title>

{{ partial "meta.html" . }}

{{ partialCached "styles.html" . }}

{{- block "head" . }}{{ end }}

{{ partial "meta_json_ld.html" . }}

{{ partial "scripts.html" . }}

{{ partial "math.html" . }}

{{ partial "gamescript.html" . }}

{{ partialCached "tracking.html" . }}

</head>

but it is not working.

Any suggestion, like I want to do?

Thank you in advance ! :smiley:

Does that mean the script isn’t working the way you want it to, or does that mean that you can’t see the rendered code when you view source in your browser?

I assume you placed the file here…

layouts/partials/
└── gamescript.html

Its not working means - after that if i build the project, it is breaking and throwing error. I am unable to run “hugo server” command.

You are more likely to receive a prompt and accurate response if you post a link to the public repository for your project.

See Requesting Help.

Let us see your code

Include a link to the source code repository of your project, because we really need the context of seeing your templates and partials to be able to help you. It is trivial to do a quick git clone on your repo, then run hugo server in your project, to help you out. On the other hand, recreating your code from screenshots, or sort of guessing at it, is not.

If you can’t share your repository for whatever reason, consider creating a dummy repo that you can share, which reproduces the problem you’re experiencing.

1 Like