2011-12-27 29 views
-1

我在C#中編寫我的sharepoint項目中的這種編碼。它運作良好。C#中的javascript問題#

ClientScript.RegisterStartupScript(this.GetType(), "ale", "window.open('DownloadHandler.ashx?fileName=Error.log');", true); 

但是,當我改變我的編碼,這不再起作用。

ClientScript.RegisterStartupScript(this.GetType(), "ale", string.Format("window.open('DownloadHandler.ashx?fileName={0}')", "Error.log"), true); 

我的編碼有什麼問題?

+2

定義「不再有效」嗎? – 2011-12-27 03:05:37

+4

我不知道,'window.open()'語句末尾有一個';'缺少嗎? – 2011-12-27 03:07:22

+1

@kevin在javascript結尾缺少分號? – Nalaka526 2011-12-27 03:08:15

回答

2

有在window.open()語句的末尾缺少;

ClientScript.RegisterStartupScript(
    this.GetType(), 
    "ale", 
    string.Format("window.open('DownloadHandler.ashx?fileName={0}');", "Error.log"), 
    true 
); 
+0

我很好奇爲什麼缺少分號會導致它停止工作,因爲[Javascript有自動分號插入](http:// mislav .uniqpath.com/2010/05 /分號/)。 – 2011-12-27 03:14:13

+0

@PeterOlson - 我不確定,因爲我在想同一件事(Javascript中的小錯誤)。如果語句沒有結束,IE可能會拋出錯誤並停止執行?我想知道同樣的事情。這是我用示例代碼看到的唯一問題。 – 2011-12-27 03:15:50

+0

腳本註冊器可能在其後添加了其他腳本,並將它們相加在一起,從而導致腳本出錯。該腳本字符串在由.net執行之前由.net處理。 – 2011-12-27 03:15:59

1

所以它缺少一個分號;底。

ClientScript.RegisterStartupScript(this.GetType(), "ale", string.Format("window.open('DownloadHandler.ashx?fileName={0}');", "Error.log"), true);