Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unknown reason for "Expected ':'" error in javascript

Tags:

javascript

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 +");");
}
like image 747
Jeff Reddy Avatar asked Apr 15 '15 15:04

Jeff Reddy


People also ask

Why do I get unexpected token error?

The JavaScript exceptions "unexpected token" occur when a specific language construct was expected, but something else was provided. This might be a simple typo.

Why am I getting a JavaScript error?

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.

What is the most common error in JavaScript?

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.

When there is a problem with JavaScript code there are generally two types of errors?

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.


2 Answers

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.

like image 159
Pointy Avatar answered Nov 03 '22 01:11

Pointy


Have you tried to put single quotes before and after closing and opening double quotes around url variable? Somthing like:

 ..." = window.open('" + url + "',... 
like image 20
Gardax Avatar answered Nov 03 '22 00:11

Gardax