2014-02-25 40 views
0

我正在使用Vex.js(它使用jquery,如果有幫助)爲我的網站設置一個小模式對話框。我將腳本鏈接到頁面,並使用getElementById(我被告知這是最佳做法)註冊一個按鈕單擊按鈕,將彈出對話框。一切都在FF罰款,但在Chrome不會在所有的工作,當我檢查控制檯我得到一個Uncaught SyntaxError: Unexpected identifier上線1列1Chrome瀏覽器的JavaScript錯誤不存在,完全破壞Javascript代碼的Firefox?

我的代碼如下所示:

function popup() vex.dialog.open({ 
    message: 'Enter your username and password:', 
    input: "", 
    buttons: [ 
     $.extend({}, vex.dialog.buttons.NO, { 
      text: 'Login' 
     }) 
    ], 
    callback: function(data) { 
     if (data === false) { 
      return console.log('Cancelled'); 
     } 
     return console.log('Username', data.username, 'Password', data.password); 
    } 
}); 


document.getElementById("sharebutton").onclick = popup; 

謝謝。

+3

我不明白這是如何在任何瀏覽器中工作的......你的'popup'函數缺少打開和關閉花括號。 –

+0

介意玩弄我....謝謝 –

+0

這行'function popup()vex.dialog.open({'是不正確的Javascript語法,我不知道你在做什麼。 – jfriend00

回答

2

由於@Jason P建議使用大括號:

function popup() { 
    vex.dialog.open({ 
     message: 'Enter your username and password:', 
     input: "", 
     buttons: [ 
      $.extend({}, vex.dialog.buttons.NO, { 
       text: 'Login' 
      }) 
     ], 
     callback: function(data) { 
      if (data === false) { 
       return console.log('Cancelled'); 
      } 
      return console.log('Username', data.username, 'Password', data.password); 
     } 
    }); 
} 

document.getElementById("sharebutton").onclick = popup; 
0

工作的呢?

function popup() { 
    vex.dialog.open({ 
     message: 'Enter your username and password:', 
     input: "", 
     buttons: [ 
      $.extend({}, vex.dialog.buttons.NO, { 
       text: 'Login' 
      }) 
     ], 
     callback: function(data) { 
      if (data === false) { 
       return console.log('Cancelled'); 
      } 
      return console.log('Username', data.username, 'Password', data.password); 
     }); 
}; 

document.getElementById("sharebutton").onclick = popup; 
1

正如別人所說,你可以通過添加大括號來修復它。

但更有趣的是爲什麼firefox不會對它有所瞭解,有些測試提示函數語法與if語句相似。

您可以使用

if (bool) { 
    statementX; 
} 

if (bool) 
    statementX; 

和同樣與功能,讓你函數的工作,因爲你的功能包括1線。

見arrow_functions:https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/arrow_functionsProtovis - What are these functions with no curly braces?

可能是因爲箭頭的功能相同的方式工作,多發言,並不需要爲Firefox支持它所有函數聲明一行bracets。

+1

有趣的是可能性確實存在於我的腦海中,但我駁斥了它。 –

相關問題