7

hello im am getting JS error :

 Uncaught SyntaxError: Unexpected identifier

here

<script type="text/javascript">
var cur_level = 1;
var ids_arr = new Array(<?php echo $max_level?>);
var im_here = new Array(<?php echo $max_level?>);
ids_arr[0] = 1;
im_here[0] = "|";
function displayData(id, level, used, title)
{
if(used){
    choice = document.getElementById('divv'+id).innerHTML;
    document.getElementById('test_div').innerHTML = choice;

} else {
    document.getElementById('test_div').innerHTML = ' No lerning paths to show.';
    updateLinksDiv(id, level, title);

  }
}

function updateLinksDiv(id, level, title)
{
var links_div_info = document.getElementById('links_parent_'+id);
var tmpHTML = '';
var here = '';

for(i=0;i<level;i++){
    here+= '->'+im_here[i];
    links_div_info = document.getElementById('links_parent_'+ids_arr[i]);
    tmpHTML += '<div id="divl_'+links_div_info.id+'">'+links_div_info.innerHTML+'</div>';
}
links_div_info = document.getElementById('links_parent_'+id);
tmpHTML += '<div id="divl_'+links_div_info.id+'">'+links_div_info.innerHTML+'</div>';

document.getElementById('links').innerHTML = tmpHTML;
ids_arr[i] = id;
im_here[i] = title;
}

</script>


<script type="text/javascript">
    window.onload=updateLinksDiv(1 , 0 , "|" ) ;
</script>

the functions are suppose to create an "expanding" that opens up with levels and everything was working fine untill i added the "title" and i started getting the error. the error points me to the last and i just cant find the error... i try to call displayData like this

onclick="displayData('.$cat->id.','.$cat->level.',0,'.$cat->title.')"

any suggestions for what i'm not seeing.?

thank you

Dvir Levy
  • 8,018
  • 11
  • 39
  • 60
  • 4
    You have a JavaScript error, please show us the JavaScript and not some PHP that will generate JavaScript. – Quentin Jan 08 '12 at 12:18
  • Also tell us which line triggers the error. – Quentin Jan 08 '12 at 12:19
  • As a side note, you should not use the `onxxx` attributes. Instead, bind the events to the elements after the page has loaded - this is called [Unobtrusive Javascript](http://en.wikipedia.org/wiki/Unobtrusive_JavaScript). Similarily, using `innerHtml` is a receipe for [XSS injection vulnerabilities](http://en.wikipedia.org/wiki/Cross-site_scripting). I strongly recommend using jQuery or a similar library. – phihag Jan 08 '12 at 12:20
  • the links the php creats are displayData(26,1,0,כיתה ג) and i get the error in the last thats where it points me to... – Dvir Levy Jan 08 '12 at 12:35
  • Does this answer your question? [SyntaxError: Unexpected Identifier in Chrome's Javascript console](https://stackoverflow.com/questions/5000114/syntaxerror-unexpected-identifier-in-chromes-javascript-console) – Michael Freidgeim Mar 20 '23 at 12:23

1 Answers1

8

In your comment you say that displayData(26,1,0,כיתה ג) is generated. This explains the symptoms, as here the last parameter contains a space in addition to Hebrew letters, so the JavaScript intepreter sees it as two identifiers separated by a space, and the identifiers are probably undefined. Google Chrome gives the error message you describe, whereas Firefox and IE say, more enigmatically, “missing ) after argument list.”

Apparently the generated code is supposed to have the last parameter in quotation marks, i.e. 'כיתה ג'. You need to modify the generation to contain them.

Jukka K. Korpela
  • 195,524
  • 37
  • 270
  • 390