2015-09-18 41 views
2

如果所有必填字段都不完整,並且是使用Google Apps腳本創建的表單的一部分,我有以下JavaScript函數可阻止表單提交。請注意,#submitbutton實際上是一個常規按鈕,Google Apps腳本會強制使用嚴格的JavaScript。該腳本在Chrome上運行良好,但是當我在Safari或Firefox上測試它時,它似乎沒有運行。我是否使用僅適用於Chrome的方法?我在這裏錯過了什麼嗎?使用jQuery在Safari和Firefox中使用Javascript(嚴格)錯誤

我收到以下錯誤在Safari: Uncaught TypeError: undefined is not a function (evaluating '(1, $)('#submitbutton')')Uncaught Strict mode does not allow function declarations in a lexically nested statement.

火狐給出了一個類似的問題:Uncaught in strict mode code, functions may be declared only at top level or immediately within another function 2699307207-maestro_htmlapp_bin_maestro_htmlapp.js:84:360Uncaught TypeError: $ is not a function

<script type="text/javascript"> 
$('#submitbutton').on('click', function() { 
    $(this).val("Submitting..."); 
    //check for required fields 
    var emptyFields = $('[required]').filter(function() { 
     $(this).removeClass("warning"); 
     if ($(this).val().length === 0){ 
     $(this).addClass("warning") 
     return true 
     } else { 
     return false 
     } 
    }); 

    if (emptyFields.length === 0) { 
     $(this).prop('disabled', true); 
     document.getElementById('incompleteWarning').style.display = 'none'; 
     document.getElementById('bePatient').style.display = 'inline'; 
     google.script.run.withSuccessHandler(fileUploaded).uploadFiles(this.parentNode); 
    } else{ 
     $(this).val("Submit Application") 
     document.getElementById('incompleteWarning').style.display = 'inline'; 
     document.getElementById('incompleteWarning').style.color = 'red'; 
    } 
}); 
</script> 

的jsfiddle這裏:https://jsfiddle.net/hofresre/

。注意的jsfiddle沒有按」 t使用google.script,但用this.parentNode.submit替換它()

任何幫助,非常感謝。

每評論:

maestro_htmlapp_bin_maestro_htmlapp.js的線84:84:360: function ys(a){for(var b=[],c=0;c<a.length;++c){var d=a[c];b[c]=Mn(d)?(new String(d)).toString():d}return b}var As=[vh,"[object Object]"];var Bs=["alert",ts(function(a){return alert(a)},16,[4]),"confirm",ts(function(a){return confirm(a)},16,[4]),"prompt",ts(function(a,b){return prompt(a,b)},16,[4,4])];var Cs=window.console&&window.console.error?function(a){window.console.error(a)}:void 0,Ds=window.console&&window.console.info?function(a){window.console.info(a)}:void 0,Es=window.console&&window.console.log?function(a){window.console.log(a)}:void 0,Fs=window.console&&window.console.warn?function(a){window.console.warn(a)}:void 0,Gs=["console.debug",ts(window.console&&window.console.debug?function(a){window.console.debug(a)}:void 0,16,[4]),"console.error",ts(Cs,16,[4]),"console.info",ts(Ds,16,[4]),

+0

with firefox can you show the line'maestro_htmlapp_bin_maestro_htmlapp.js:84:360'? – tnga

+0

@arsel,你只想要84行嗎?我將其附加到原始帖子。如果這不是你要求的,請原諒我的無知。 – LJB

回答

0

我猜你應該知道的瀏覽器不支持網絡功能的相同的方式。我們知道谷歌總是首先爲Chrome瀏覽器集中他們的網絡庫和他們的支持。

當你只有這個錯誤:

functions can only be declared at top level or immediately within another function

你不能把一個函數聲明任何其他塊內,像一個if語句for循環;導致使用大括號任何塊範圍:的try-catch-終於的if-else if-else語句開關ES6還定義了新的功能,利用模塊的範圍,例如類。

不能:"use strict" { Function bar(){/****/} }

因此,獨立的解決方案,我可以提供有:

  • 查找,如果有一個版本的腳本(這裏谷歌腳本或JQuery的)(這是最新的)這個問題已經解決了。
  • 您也可以在相應的腳本中(例如在google腳本中)發表評論"use strict"
  • 表明您首先在其他js腳本之前包含google腳本和JQuery。
  • 嘗試使用非縮小腳本庫,因爲某些東西(附帶JQuery 1.11的示例)是導致問題的原因。

之前嘗試以前的解決方案,也許這個修改後的代碼可以工作:

<script type="text/javascript"> 

var trySubmission = function() { 
$(this).val("Submitting..."); 
//check for required fields 
var emptyFields = $('[required]').filter(function() { 
    $(this).removeClass("warning"); 
    if ($(this).val().length === 0){ 
    $(this).addClass("warning") 
    return true 
    } else { 
    return false 
    } 
}); 

if (emptyFields.length === 0) { 
    $(this).prop('disabled', true); 
    document.getElementById('incompleteWarning').style.display = 'none'; 
    document.getElementById('bePatient').style.display = 'inline'; 
    google.script.run.withSuccessHandler(fileUploaded).uploadFiles(this.parentNode); 
} else{ 
    $(this).val("Submit Application") 
    document.getElementById('incompleteWarning').style.display = 'inline'; 
    document.getElementById('incompleteWarning').style.color = 'red'; 
} ; 
} 

$('#submitbutton').on('click', trySubmission) ; 

</script> 
+0

我正在開發Google Apps腳本,因此我無法改變他們在HTML服務中強制使用嚴格JavaScript的事實。我試着改變你所建議的代碼,但它不起作用;我仍然有兩個錯誤。 '<! - Load jQuery Library - > '是我在我的html頁面頂部使用的jQuery,所有用戶定義的函數都在最下面。我並不十分熟悉縮小,也不確定我正在使用的jQuery庫是否縮小。 – LJB

+0

你的jQuery庫被縮小會導致* .min。*出現在擴展名之前。 – tnga

0

什麼模式,你加載HTML從GS文件?

當SandboxMode.NATIVE在SandboxMode.IFRAME事情發生錯誤似乎工作

下面是一個GS文件

function doGet(e) { 
    var html= HtmlService.createTemplateFromFile("test.html"); 
    return html.evaluate().setSandboxMode(HtmlService.SandboxMode.IFRAME); 
} 

一些測試代碼...和HTML作爲的test.html

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> 
<script> 

    function myFunction() { 

    if (jQuery) { 
    // jQuery is loaded 
     console.log("Loaded"); 
    } else { 
    // jQuery is not loaded 
     console.log("Not Loaded"); 
    } 
    $("#message").html("Something happened."); 
    } 

    $(document).ready(myFunction); 

</script> 
<p id="message">Nothing happened. </p> 
相關問題