Script tags that are included within the element Vue vm is bound to can result in a template error

Here is the Laravel layout template that I am currently working with:

<html lang="en">
<head>
    <meta name="csrf-token" content="{{ csrf_token() }}">

    <link href="{{ URL::asset('css/libs.css') }}" rel="stylesheet">

    <link href="{{ URL::asset('css/app.css') }}" rel="stylesheet">

    @yield('styles')

    @yield('scripts.header')

 </head>
 <body>

    <div id="app">

        <div class="main-container">
            <div class="ad ad-leaderboard hidden-xs">
                <script>
                       function onSubmit(token) {
                           $("#register-form").submit();
                       }
                </script>
            </div>

            @include('layouts.partials.header')

            <div id="main">
                @yield('content')
            </div>

            @include('layouts.partials.footer')

        </div>
    </div>

    <!-- Jquery, Bootstrap, Moment, DatetimePicker, Sweetalert, Select2 -->
    <script src="{{ URL::asset('js/libs.js')}}"></script>

    <!-- Custom -->
    <script src="{{ URL::asset('js/app.js')}}"></script>

    @yield('scripts.footer')


   </body>
</html>

The section

<script src="{{ URL::asset('js/app.js')}}"></script>
contains Vue setup as follows:

const app = new Vue({
   el: '#app'
});

I am looking to include Google Ad scripts in my content but encounter a

[Vue warn]: Error compiling template:
error in the console.

Since I only use Vue components on specific pages, I am considering moving the Vue declaration out of app.js and onto those individual pages. However, I am curious if there is a more efficient way to organize this to avoid repeating the declaration on every separate page while still being able to inline JavaScript within the main layout page. Any suggestions?

Answer №1

The issue stems from the presence of the <script> tag within the #app div.

<script>
    function onSubmit(token) {
        $("#register-form").submit();
    }
</script>

To resolve this, it is recommended to relocate this code snippet to the end of the document along with other JavaScript functions.

UPDATE

Another approach worth considering is utilizing a method such as integrating scripts into Vue components.
https://github.com/TheDynomike/vue-script-component

This allows for the creation of Vue components dedicated to individual ad scripts, which can be easily inserted into the desired locations on the page.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is your Jquery validation malfunctioning?

I am currently facing a challenge with required validation on an asp.net page. In order to validate the inputs, I have implemented multiple controls which can be hidden or displayed based on certain conditions. These controls include checkboxlists, dropdow ...

triggering a touchend event using jQuery Mobile

I'm currently utilizing jQuery Mobile in my project. Is there a way to terminate a gesture using jQuery Mobile? Let's say a user places their fingers on the screen and performs a drag - can I end this action programmatically with JavaScript, eve ...

Tips for utilizing vulnerable web scripts on SSL-enabled pages

After implementing SSL to secure my pages, I encountered an issue where one of my scripts no longer functions properly. Specifically, I had a script on my page that was used to display the visit count from this website. Previously, it was functioning fla ...

Displaying a certain div when clicked within a loop using Vue.js and Laravel

I am currently facing an issue with displaying a hidden div upon click. The problem arises when using a loop to dynamically generate all the divs. Whenever I click the button, it shows all the divs instead of just one specific div on each click. I attempte ...

Access basePath within the Next.js AppRouter

Is there a way to access the basePath in Next.js 13 when using AppRouter? To retrieve it, we can simply get router.basePath through the useRouter hook of PageRouter by importing `import { useRouter } from 'next/router' I am currently unable to ...

Ways to eliminate all attributes and their corresponding values within HTML tags

Hey there, I'm trying to strip away all the attribute values and styles from a tag in html Here's my Input: <div id="content"> <span id="span" data-span="a" aria-describedby="span">span</span> <p class="a b c" style=" ...

Passing two variables through a Javascript URL to dynamically update a dropdown menu based on the specified values

What is the best way to send two variables to a JavaScript function in order to modify the dropdown list according to those values? $(document).ready(function() { $("#date").change(function() { $(this).after('<div id="loader">& ...

Is there a more efficient way to handle post data without increasing its size when encoding JSON within a URI

I have a significant amount of data to post, and it needs to be minimized for performance reasons. Initially, my data consists of JavaScript objects which I then convert to JSON format before sending it via POST request. The issue is that my data include ...

Activate a button upon the user clicking the Enter key

Is there a way to automatically trigger a button click when the user presses the Enter key inside an input field? I've attempted to achieve this functionality, but have been unsuccessful so far. Here is my code snippet: <!DOCTYPE htm ...

Seeking out a particular key within a JSON object and then finding a match based on the id within that key's array - how can it be

Recently, I've been exploring JavaScript and encountering challenges when trying to apply array methods on objects. For instance, I received a fetch response and my goal is to extract the 'entries' key and then utilize the native Array.find( ...

Tips for implementing two Secondary Actions in React Material UI, with one positioned on the left corner and the other on the right, while ensuring that the behavior of the

Is there a way to display two secondary actions on either end of a list item without triggering additional onclick events? The placement can be adjusted by overriding the CSS properties right and left. https://i.stack.imgur.com/rhr7N.gif The issue arises ...

Tips and tricks for implementing vuetify tooltips within a list component

Looking for help with my code: <div id='app'> <v-app> <v-list-tile-content> <v-list-tile v-for="(item, index) in items" :key="item.id" > <v-list-tile-action> {{index+1}}. </v-list-tile-action> < ...

Border Effect Resembling Windows Tiles

When you hover your mouse over a tile in Windows, a cool effect happens: Hovering outside the tile, but within the container area: https://i.sstatic.net/yZk9f.png Hovering inside a tile: https://i.sstatic.net/LoSmk.png Hovering outside a tile at its m ...

What is the best way to retrieve the content of HTML tags from a string using JavaScript or AngularJS?

I have a string with mixed content of HTML tags and text. Here is an example: var str = "<p></p><p><i>blabla</i></p><p><i><b>blaaaaaablaaaaa</b></i></p><iframe src="..." height=" ...

Javascript does not have the capability to interact directly with HTML code

I am facing an issue with my JSP code that is supposed to validate password and confirm password before submitting the form to a Java servlet. The problem is, even though I have written the validation script, it does not show alert messages or focus on t ...

Error: The constructor for JsSHA is not valid for the TOTP generator

Attempting to create a TOTP generator similar to Google's timed-based authenticator using the React framework. I am utilizing the bellstrand module for TOTP generation, but I am encountering issues when trying to import it into my React code. Here is ...

What is the method of using "*" as the value for allowedModules in a jsreport configuration file to enable all modules?

I am having an issue when trying to generate a report using jsreport STUDIO. The error message I received is as follows: An error occurred - Error during rendering report: Unsupported module in scripts: request. To enable require on a particular module, ...

What is the best method for storing pug-formatted data in a database?

I am currently developing a JavaScript application where I utilize pug for templates and diskdb for storing data. Within my content, I have implemented pug syntax which may appear in the following format: p some content here p some more content here p: # ...

Exploring the functionalities of using Script in Next.js 13

I want to include a vanilla JavaScript script in my next.js application like this: import Script from "next/script"; <Component {...pageProps} /> <Script id="raychat-widget" strategy="afterInteractive&quo ...

creating a multi-page form using HTML and JavaScript

I need help creating a multi-page form with a unique tab display. The first page should not have a tab-pill, while the following pages should display tabs without including the first page tab. Users can navigate to the first page using only the previous b ...