JavaScript on page


#1

Hello,

I’m creating a complex website and I’m using in general jQuery. On some pages (markdown files) I would like to add a document.ready function . How can I put some JavaScript code inside the markdown file?
My first idea is to create a page property, which is put inside the header if it is set. But is there any buildin functionality to put page specialized JavaScript code to a page?

Thx


[SOLVED] Javascript in shortcodes
#2

@flashpixx

https://discuss.gohugo.io/t/javascript-in-blogpost-but-not-on-all-sites-at-the-same-time/3978/7


#3

Thanks for that, but I understand it in this case, that I can define a JavaScript file and include it by a block structure, but in my case I would like to add “inline JavaScript code”.


#4

You can include regular HTML in you Markdown files. So a <script> tag should work. Maybe it can happen that Go templates escape them for security reasons (unless Hugo trusts the user and marks it as safe by default).


#5

@flashpixx

https://gohugo.io/templates/functions#safejs


#6

Yes the problem are the quotes. I have got this code e.g.

<script>
$(document).ready(function() {
     $("#animatefsm").click( {
	console.log("foo");
 } );
}
</script>

And the double-quotes within the jQuery call are escaped. How can I avoid the escaping within the markdown file of this block?

Thanks


#7

… just an idea, … not sure/didn’t try - about that :

{{ `<script>
$(document).ready(function() {
$("#animatefsm").click( {
console.log(“foo”);
} );
}
</script>` | safeJS }}

note: use backticks not apostrophes