. Advertisement .
..3..
. Advertisement .
..4..
HTML is considered as the excellent additional language to create Web pages. So if you find the console publish “Uncaught SyntaxError: Unexpected identifier” problem. That is one of the common errors with html in the javascript. In this article, we will help you on how to correct this error with html in the Javascript. Don’t miss this post to find the solution to fix it well.
Let’s find the causes of the error and find the way to solve it in the following part.
How did the error “Uncaught SyntaxError: Unexpected identifier” happen?
When you changed the moveElement function and adjusted the parameter, it moved to the element from the elementID. You moved DOM to the moveElement function from the positionMessage function directly. While moving that functions, the Chrome continues to display uncaught Syntax issue:
Uncaught SyntaxError: Unexpected identifier
Below is the function that I created it in with positionMessage and moveElement and it can be the cause for this problem.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</script>
<script type="text/javascript" src="scripts/positionMessage.js"></script>
<script type="text/javascript" src="scripts/moveElement.js"></script>
</head>
<body>
<p id="message">Whee!</p>
</body>
</html>
Here is an command of moveElement.js as below:
function moveElement(element,finx,finy,interval){
var xpos = parseInt(element.style.left);
var ypos = parseInt(element.style.top);
if(xpos == finx && ypos == finy) return true;
if(xpos > finx) xpos--;
if(xpos < finx) xpos++;
if(ypos > finy) ypos--;
if(ypos < finy) ypos++;
element.style.left = xpos+"px";
element.style.top = ypos+"px";
var repeat = "moveElement("+element+","+finx+","+finy+","+interval+")";
var movement = setTimeout(repeat,interval);
}
And the positionMessage.js function that you use to move to another function. Below is the positionMessage.js function:
window.onload = positionMessage;
function positionMessage(){
var elem = document.getElementById("message");
elem.style.position = "absolute";
elem.style.left = "50px";
elem.style.top = "100px";
moveElement(elem,200,100,10);
}
Solution to resolve the “Uncaught SyntaxError: Unexpected identifier” issue when console displays it.
To solve the “Uncaught SyntaxError: Unexpected identifier” eror, make sure you don’t have any name of the function. If the problem is creating the above functions, replace it with others. The best solution is to change extra arguments toward setTimeout, after that it will be moved to the right function.
A built-in JavaScript function is known as setTimeout. It starts a timer (a millisecond-based countdown) for executing a callback function and calls the function when the timer expires. The setTimeout method in JavaScript can be helpful in a variety of circumstances. SetTimeout can be used, for instance, to play a sound one second after an online game’s victory is announced or to display a “Welcome” popup two seconds after a user reaches your website. The setTimeout method can be used; however, you see fit. The callback method’s execution concludes when the setTimeout function runs only once per call, at which point setTimeout’s work is complete.
Syntax of setTimeout:
setTimeout(function, milliseconds);
Let try this command first:
function moveElement(element,finx,finy,interval){
var xpos = parseInt(element.style.left);
var ypos = parseInt(element.style.top);
if(xpos == finx && ypos == finy) return true;
if(xpos > finx) xpos--;
if(xpos < finx) xpos++;
if(ypos > finy) ypos--;
if(ypos < finy) ypos++;
element.style.left = xpos+"px";
element.style.top = ypos+"px";
var movement = setTimeout(function () {
moveElement(element, finx, finy, interval);
}, interval);
}
Delivering the extra arguments that you could move to setTimeout. Find the following argument:
function moveElement(element,finx,finy,interval){
var xpos = parseInt(element.style.left);
var ypos = parseInt(element.style.top);
if(xpos == finx && ypos == finy) return true;
if(xpos > finx) xpos--;
if(xpos < finx) xpos++;
if(ypos > finy) ypos--;
if(ypos < finy) ypos++;
element.style.left = xpos+"px";
element.style.top = ypos+"px";
var movement = setTimeout(moveElement, interval, element, finx, finy, interval);
}
Conclusion
Through the simple tip above, we believe that you can confidently correct the problem if the console displays Uncaught SyntaxError: Unexpected identifier error. Please take note or save this useful information for you and your classmate as well. Because, you can easily find this great solution quickly when needed!
Read more
→ Tips On Dealing With The Error “Uncaught SyntaxError: Identifier ‘a’ has already been declared”
Leave a comment