我在我的站点中定义了以下函数。它对某些人有效,对其他人无效。异常发生在方法的最后一行,连接所在的位置。我相信这是因为指定查询字符串的 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 将抛出错误。
关于javascript - Unknown reason for "Expected ' :'"javascript 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29654542/