簡體   English   中英

javascript中“預期':'”錯誤的未知原因

[英]Unknown reason for “Expected ':'” error in javascript

我在我的網站中定義了以下功能。 它適用於某些人,而不適用於其他人。 該方法的最后一行發生異常,其中串聯是。 我相信,因為指定查詢字符串的url的問號字符被視為三元運算符。

這里有什么東西我沒有看到,或者有更好的方法來構建這個字符串?

url變量的值為:“mywebpage.aspx?AccountNumber = 123456”

function popUp(url) {
    var myleft = (screen.width) ? (screen.width - 750) / 2 : 100;
    var   mytop = (screen.height) ? (screen.height - 300) / 2 : 100;
    var id = new Date().getTime();

    eval("page" + id + " = window.open(" + url + ", '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=900,height=325, top='" + mytop + "',left='" + myleft +");");
}

您將通過避免eval()消除“引號內的引號”問題:

window["page" + id] =
    window.open(url, id, 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=900,height=325, top=' + mytop + ',left=' + myleft);

您還應確保使用“id”值作為有效標識符(具體以非數字字符開頭),否則Internet Explorer將拋出錯誤。

您是否嘗試在關閉之前和之后放置單引號並在url變量周圍打開雙引號? 像這樣的東西:

 ..." = window.open('" + url + "',... 

使用encodeURIComponent(url)而不是在window.open中使用純url

此功能對特殊字符進行編碼。 另外,它編碼以下字符:,/? :@&= + $#

更多信息在這里

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM