2014-01-21 200 views
0

每當我嘗試點擊以下鏈接時,我都會在Chrome開發人員工具控制檯中看到(未捕獲的SyntaxError:意外的標識符錯誤)。我的代碼中看不到任何錯誤,所以有人可以檢查它並告訴我我在這裏錯過了什麼嗎?未捕獲的語法錯誤:意外的標識符錯誤

<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> 

下面是warningMsg功能,遺憾的是它並不甚至稱,但包含它包含在HTML頁面上面的鏈接.js文件:

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

注:我試圖逃離「在不能這樣做它可以\'t,但我得到了同樣的錯誤

感謝您的時間和精力

+0

嘗試使用'window.location = url + actionType +「/?id =」+ id;'而不是 – RononDex

+1

1.注意你的引號2.不要使用內聯javascript :) – Andreas

+0

@RononDex你是一個跑步者。 ..我聽過故事,但我不相信他們是真的。 –

回答

3

這是因爲在這個詞的'的‘不能’ - 它的原因你的報價是不匹配的。

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

要解決這個問題,你需要要麼逃避在' 「不能」 用一個反斜槓:

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

試圖逃離'在不能不能,但我仍然得到同樣的錯誤...任何建議?謝謝 – MChan

+0

@MChan它對我很好:http://jsfiddle.net/JamesD/DnbRk/ –

+0

@JamesDonnelly他不能真正包裝在不同的報價中,而不會弄亂HTML屬性。國際海事組織,對這樣的問題的任何答案應包括「不要使用內聯js」。 – m59

0

嘗試This step can\'t be undone!

,而不是This step can't be undone!

0

window.location.assign(url+actionType+"/?id="+id);您會得到類似於以下的結果:

/invoicing/users/?id=2 

由於此:url+actionType+"/?id="+id

當然,除了引用別人提到的引用轉義問題外,這也是。

1

您在can't有未轉義的單引號。它應該是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> 

但是,不應該使用內聯js(javascript在你的html中)(除非用於快速測試目的)。閱讀其中的一些結果:Why is inline js bad?

取而代之的是,將javascript附加到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.

0

取代:

<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> 

到:

<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> 

反正,我通常建議你使用事件處理程序的代替內嵌代碼(的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'); 
}); 

希望有幫助。

+0

幹得不錯,內聯js,但你建議'onlick',而不是首選'addEventListener'。 – m59

+0

你是對的 - 固定的 – geevee

相關問題