简体   繁体   中英

Uncaught SyntaxError: Unexpected identifier error

Whenever I try to click on the link below I get (Uncaught SyntaxError: Unexpected identifier error) in the Chrome developer tools console. I can't really see anything wrong in my code, so can someone please check it and tell me what I am missing here?

<a href="javascript:warningMsg('Are you sure you want to delete this User? This step can't be undone!','/invoicing/users/','2','delete-user')">Delete User</a>

Below is the warningMsg function, unfortunately it is not even called but the .js file containing it is included in the html page with link above:

function warningMsg(warningMsgContent, url, id, actionType){
 alert(url+actionType+"?id="+id);
 window.location.assign(url+actionType+"/?id="+id);
}

Note: I tried escaping the ' in Can't by doing it can\\'t but I get the same error

Thanks for your time and efforts

It's because of the ' in the word "can't" - it causes your quotes to be mismatched.

'Are you sure you want to delete this User? This step can't be undone!'
^                                                        ^            ^

To fix this you'll need to either escape the ' in "can't" using a backslash:

'Are you sure you want to delete this User? This step can\'t be undone!'

You have an unescaped single quote in can't . It ought to be can\\t .

<a href="javascript:warningMsg('Are you sure you want to delete this User? This step can\'t be undone!','/invoicing/users/','2','delete-user')">Delete User</a>

However, inline js (javascript in your html) should never be used (unless for quick testing purposes). Read some of these results: Why is inline js bad?

Instead, attach the javascript with javascript!

<a id="delete-user">Delete User</a>

JavaScript:

var d = document.getElementById('delete-user');
d.addEventListener('click', function() {
  warningMsg(
    "Are you sure you want to delete this User? This step can't be undone!",
    '/invoicing/users/',
    '2',
    'delete-user'
  );
});

Here's a live demo you can mess with.

try This step can\\'t be undone!

instead of This step can't be undone!

In window.location.assign(url+actionType+"/?id="+id); you will yield results similar to the following:

/invoicing/users/?id=2

Due to this: url+actionType+"/?id="+id .

This is, of course, in addition to the quotation escape issue everybody else has mentioned.

replace:

<a href="javascript:warningMsg('Are you sure you want to delete this User? This step can't be undone!','/invoicing/users/','2','delete-user')">Delete User</a>

to:

<a href="javascript:warningMsg('Are you sure you want to delete this User? This step can\'t be undone!','/invoicing/users/','2','delete-user')">Delete User</a>

(needed to escape the ' in the word can't)

anyway, i'd generally suggest you to use event handlers instead of inline code (onclick=...):

html:

<a id="btn_delete" href="#">Delete user</a>

javascript:

document.getElementById('btn_delete').addEventListener('click', function() {
    warningMsg('Are you sure you want to delete this User? This step can\'t be undone!','/invoicing/users/','2','delete-user');
});

hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM