Sky Daddy
Sky Daddy

Reputation: 11

How to use inline JavaScript inside HTML files?

I would like to use JavaScript tags inside HTML files in Electron.

I know I can use code like this in main.js:

webPreferences: { preload: path.join(__dirname, "preload.js"), },

And then import this script in HTML files like this:

<script>alert("This works!")</script>

But why can't I just use JavaScript like this inside HTML files:

alert("Why won't this work?");

There is a default meta tag in HTML when I follow Electron's quick start guide: <meta http-equiv="X-Content-Security-Policy" content="default-src 'self'; script-src 'self'" />

And this I guess disables executing inline JavaScript.

So my question is, is it bad practice to use inline JavaScript like this in Electron? Should this be avoided?

Upvotes: 1

Views: 928

Answers (1)

A.J.Sarmah
A.J.Sarmah

Reputation: 11

yes you can use javascript inside HTML files by using the script tag .

<script>
     //code goes here
</script>

now coming to use of inline javascript its not a good practice if you are creating a project because then it will be hard to maintain if you start working with multiple files.

Upvotes: 1

Related Questions