Only a simple verification is required - Uncaught SyntaxError: Unexpected token else

I am not a JavaScript developer and I'm facing an issue (Uncaught SyntaxError: Unexpected token else)

on one of our web pages ...

I strongly believe this is a repetition of 2 previously answered questions in similar topics:

1) Syntax Error: Unexpected token Else 2) Prompt JavaScript If Else Unexpected Token else

So, I admit my mistake ... However, I need to confirm if my case completely matches the referenced cases by checking for a trailing ;

Here's the script causing the issue:

<script type="text/javascript">
if("0" == 0) var t = 'existing' else var t = 'new';
document.write('<scr'+'ipt type="text/javascript" src="//my.domain.com/tag.js?ck=XXX&Retarget=domain:'+t+':psd:sku:XXXXXXXX"></scr'+'ipt>');
</script>

In Chrome, the error occurs after the if statement

Answer №1

Ensure to include the ; at the end of your command. When using multiple commands in a line, remember to terminate each command with ; Here is the corrected version:

<script type="text/javascript>
if("0" == 0) var t = 'existing'; else var t = 'new';
document.write('<scr'+'ipt type="text/javascript" src="//my.domain.com/tag.js?ck=XXX&Retarget=domain:'+t+':psd:sku:XXXXXXXX"></scr'+'ipt>');
</script>

In any case, it is advisable to use brackets {} instead of the abbreviated version

Answer №2

When comparing "0" to 0, the variable t is set to 'existing' if true, and 'new' if false.

Answer №3

You should consider the following:

if("1" === 1) 
{
   var result = 'match'; 
}
else 
{
   var result = 'mismatch';
}

It is quite uncommon to compare "1" === 1.

Answer №4

Perhaps it would be more efficient to utilize an inline if statement for defining the variable "t," which can also resolve your issue

<script type="text/javascript>
        var t = ("0" == 0) ? 'existing' : 'new';
        document.write('<scr'+'ipt type="text/javascript" src="//my.domain.com/tag.js?ck=XXX&Retarget=domain:'+t+':psd:sku:XXXXXXXX"></scr'+'ipt>');
    </script>

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

How can I ensure that $routeProvider functions correctly within my AngularJS application?

I'm currently in the process of manually constructing a shell and trying to understand its functionality Shell Structure: - application (contains PHP files) - webroot -- app --- app.js -- templates --- main ---- login ----- login.html index.html ...

When attempting to invoke a JavaScript function on JSP, there seems to be no

I am currently developing a web application and delving into the intricacies of how Ajax functions within it. I am encountering an issue where nothing occurs when I attempt to select a category, and no errors are being reported. Within my JSP page: < ...

AngularJS click directives and Google Chrome's content protection policy

Here is an interesting scenario I encountered recently. The new Google Chrome's Content Security Policy now prohibits the use of inline scripts or event handlers like onclick or ontouch within the HTML itself. This means that we are required to write ...

SVG Filter: The image is not completely covered by a width and height of 100%

Here's a basic SVG Filter. Click the example below to toggle the appearance of the filter: var image = document.querySelector('image'); var url = 'https://i.picsum.photos/id/202/2 ...

Determine if a specific date occurred at least one day ago using momentjs

Is there a way to determine if a specific date is at least one day (24 hours) in the past using momentjs? For example: const currentDate = moment() const isAtLeastOneDayAgo = currentDate.subtract(dateToVerify) > 1 // How can this be done? ...

Encountering Reference Error while Using AWS Amplify with Nuxt.js: Navigator Undefined

Currently, I am experimenting with Nuxt.js and AWS Amplify to leverage the benefits of SSR/SEO for my project. I have successfully integrated Amplify into my project and followed the "Manual Configuration" steps outlined in the Amplify Docs to set it up. ...

What is the reason behind "Script" being considered the offspring of "Body"?

Unfortunately, I struggle with HTML/CSS/Javascript and am just trying to get through my exams. I have the code snippet below: <script> window.onload=function() { var c = document.body.childNodes; var txt = ""; var i; for ...

"Enabling image uploads in Vue.js with TinyMCE: A step-by-step guide

I have integrated tinymce into my Vue.js application, but I am experiencing issues with uploading images. I have included the following package in my project: import Editor from '@tinymce/tinymce-vue'. I believe I might be missing a necessary pl ...

What is the method for obtaining the Express type from the node_modules directory?

I was eager to utilize JSDoc for annotating my function argument. Here's what I did: import { Express } from '@types/express-serve-static-core'; /** * @param {Express} app */ function install(app) { app.post('/auth/login&apos ...

"Incorporate a new dropdown menu and then append the selected item to an array within

I am trying to enhance my user interface by adding two new select boxes (from and until) dynamically using ng-click, but I am facing challenges in binding these selects to my array in the scope. Here is the HTML code snippet: <div ng-repeat="time in a ...

How can we incorporate a Material-UI skeleton to display API response data effectively?

I have incorporated the material-ui skeleton (Shimmer effect) to display data fetched from an API. { accountData.usersAccountData.map((userData, index) => ( // ...UI code ) ) } The output is shown below : https://i.sstatic.net/MEVhA.png It can be o ...

Tips for securely implementing JSON web tokens when integrating an external application with the WordPress REST API

I have a query regarding JWT. Let's consider this situation. A -> wordpress site with wp rest api enabled; B -> External application (for example, a simple javascript/jQuery app) Suppose I want to make a post request or create a new post on t ...

The jQuery .html() method cannot be used to replace a JavaScript chart

Utilizing jQuery's ajax calls to generate a Dojo toolkit chart based on the user's selection from a drop-down menu is proving tricky. The issue arises when attempting to replace the existing chart - instead of swapping it out, the new chart appea ...

Tips for automatically choosing several choices from a multiple-select using Chosen.JS

I am struggling to programmatically select multiple values in a multiple select using chosenJS/chosen.js while still ensuring that the selected values are bound to my ng-model (angularJS). In this scenario, I have a list of users, some of whom are already ...

Alter the position of the anchor object in the center using JavaScript upon page load

When my page loads, I want to automatically jump to a specific anchor tag and ensure that the anchor object is centered in the window. Initially, I implemented a basic function to achieve this: <body onload="goToAnchor();"> <script type="text/ja ...

establish a preset selection as checked if the countryCode indicates the United States

Is there a way to automatically set the default checked value of Fitting to 'US' when the countryCode =='US' in this scenario? Here is the code snippet: export default function ProductOptionsDefault({ name, values, countryCode, } ...

Exploration of JavaScript Interview Questions

I need help creating a JavaScript function that will be triggered on button click, but instead of targeting the button directly, it should target a div element by its class. How can I achieve this? <body> <div class="content"> <butto ...

What steps should I follow to effectively store this JSONB data in PostgreSQL by utilizing node-postgres (pg)?

After receiving information in the GET URL, I need to pass it into JSON format and save it with increasing IDs into a PostgreSQL database. However, the code I wrote does not seem to be saving anything without any errors: // Initializing Pg const { Client ...

Ways to safeguard function constructor arrays from unauthorized alterations using get methods in JavaScript

I am dealing with a coding challenge that involves managing state in JavaScript. Here is the code snippet I have been working on: function State(){ const SecretState = { name: 'bob' } this.getState = () => SecretState; this.setStat ...

Executing a Javascript GET request leading to a 301 redirect

Struggling to identify the issue at hand, I have turned to this platform in hopes of finding a solution. The setup involves an angularJS app with a GoLang/Gorilla mux server backend. The web app runs on http://localhost:8888/, while the server operates at ...