I have the following function defined in my site. It works for some people, not for others. The exception occurs on the last line in the method, where the concatenation is. I believe that because url's question mark character designating the query string is being looked as a ternary operator.
Is there something here that I'm not seeing, or is there a better way to build this string?
The url variable has a value of : "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 +");");
}
The JavaScript exceptions "unexpected token" occur when a specific language construct was expected, but something else was provided. This might be a simple typo.
Grammatical mistakes, such as missing parentheses or unmatched brackets, are the major causes of syntax errors in JavaScript. For example, when you must use conditional statements to address multiple conditions, you may miss providing the parentheses as required, leading to syntax flaws.
TypeError is one of the most common errors in JavaScript apps. This error is created when some value doesn't turn out to be of a particular expected type. Some of the common cases when it occurs are: Invoking objects that are not methods.
Namely, with JavaScript, most of our errors fit into two categories: syntax errors and runtime errors. A syntax error is a problem with the grammar in our code. Syntax errors mostly come in the form of misspelled keywords, missing or open brackets, or missing parentheses or punctuation.
You'll eliminate the "quotes within quotes" problem by avoiding 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);
You should also make sure you use an "id" value that serves as a valid identifier (starts with a non-digit character, specifically) or else Internet Explorer will throw an error.
Have you tried to put single quotes before and after closing and opening double quotes around url variable? Somthing like:
..." = window.open('" + url + "',...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With