13

I have a script to open a model window.. Chrome gives me "Uncaught SyntaxError: Unexpected token }" on a line that doesn't even have a closing curly brace.

Here is the portion of the script that has the error:

function showm(id1){
  window.onscroll=function(){document.getElementById(id1).style.top=document.body.scrollTop;};
  document.getElementById(id1).style.display="block";
  document.getElementById(id1).style.top=document.body.scrollTop;
}

Does anybody have any ideas on this? Any help is appreciated.

Tanner Ottinger
  • 2,970
  • 4
  • 22
  • 28
  • 4
    I can't see anything wrong with that (except that you are attempting to assign integers to CSS properties that require lengths). I'm guessing the error is in some surrounding code. – Quentin Dec 20 '10 at 16:21
  • 1
    That code you've posted does not cause a syntax error. Is that really **all** of your script? Nothing in that code looks like it has anything to do with opening a window. – Pointy Dec 20 '10 at 16:23
  • it doesnt really open a window, it just makes a div, the modal "window", visible. and no, it isnt all. but the rest of the code isnt where the problem is. and plus, it like 100 lines. would you like me to post that? – Tanner Ottinger Dec 20 '10 at 16:26
  • @tann98 the code you posted is also not where the problem is. – Pointy Dec 20 '10 at 16:38
  • i really wouldnt know where to look, if not there. chrome says line 2, and what i posted, started on line 2. line 2 is where the function starts. here is the full code, too: http://ft2.hostei.com/source.htm right click, view surce – Tanner Ottinger Dec 20 '10 at 16:45
  • @tann That code has 8 JSLint errors. First deal with those errors. – Šime Vidas Dec 20 '10 at 16:59
  • @tann98 Oh dear, that page now gets redirected to http://www.000webhost.com/admin-review which says it is being checked for malicious content. You're not having much luck today... – Day Dec 20 '10 at 17:02
  • @day oops. nothing malicious though, so it hopefully wont be stiffed. @Sime Vidas fixed almost all of those, those come the the mindset of a PHP programmer. – Tanner Ottinger Dec 20 '10 at 17:08
  • 1
    Can you try pasting the entirety of the JavaScript code into a http://jsfiddle.net example? – Day Dec 20 '10 at 17:09
  • here: http://jsfiddle.net/3xw3v/3/ – Tanner Ottinger Dec 20 '10 at 17:19
  • @tann98 Google Chrome doesn't give me any errors in the JavaScript Console when I view http://jsfiddle.net/3xw3v/3/. Do you still see the error? I'm using 8.0.552.224 beta on Ubuntu – Day Dec 20 '10 at 17:37
  • Yep no error and the function is loaded, I've added `alert(showm);` and it show the function just fine. I fear you'll have to give us link to live example where that error occurs. – Shadow The GPT Wizard Dec 20 '10 at 19:11
  • making it. its at http://ft2.hostei.com/ft.v1/. click on edit, for one of them. feel free to make a new page to experiment on. click on one of the edit links, and press submit. thats when its fired. – Tanner Ottinger Dec 20 '10 at 19:22
  • @Shadow Wizard : yes, but i am calling it within a function, AJAX. i dont know what could be wrong. i did a test, and the error appears on line 2, no matter whats there, aft click: *anyfile* >> edit >> submit. i think that its being conditioned in a way that is making line 2 have an error. however, i cant amagine what could be doing that, and in such a way. – Tanner Ottinger Dec 20 '10 at 20:01
  • @tann98 I've found a definite problem and updated my answer, please check that out – Day Dec 21 '10 at 16:52

2 Answers2

21

Try running the entire script through jslint. This may help point you at the cause of the error.

Edit Ok, it's not quite the syntax of the script that's the problem. At least not in a way that jslint can detect.

Having played with your live code at http://ft2.hostei.com/ft.v1/, it looks like there are syntax errors in the generated code that your script puts into an onclick attribute in the DOM. Most browsers don't do a very good job of reporting errors in JavaScript run via such things (what is the file and line number of a piece of script in the onclick attribute of a dynamically inserted element?). This is probably why you get a confusing error message in Chrome. The FireFox error message is different, and also doesn't have a useful line number, although FireBug does show the code which causes the problem.

This snippet of code is taken from your edit function which is in the inline script block of your HTML:

var sub = document.getElementById('submit');
...
sub.setAttribute("onclick", "save(\""+file+"\", document.getElementById('name').value, document.getElementById('text').value");

Note that this sets the onclick attribute of an element to invalid JavaScript code:

<input type="submit" id="submit" onclick="save("data/wasup.htm", document.getElementById('name').value, document.getElementById('text').value">

The JS is:

save("data/wasup.htm", document.getElementById('name').value, document.getElementById('text').value

Note the missing close paren to finish the call to save.

As an aside, inserting onclick attributes is not a very modern or clean way of adding event handlers in JavaScript. Why are you not using the DOM's addEventListener to simply hook up a function to the element? If you were using something like jQuery, this would be simpler still.

Day
  • 9,465
  • 6
  • 57
  • 93
  • thanks! this error has wasted so much of my time! thanks again! – Tanner Ottinger Dec 21 '10 at 16:57
  • @tann98 You're welcome. Start using `addEventListener` instead to hook things up to functions which are defined in your JavaScript file and you'll get proper line numbers for your syntax errors from now on - would have saved you hours on this problem alone :) Best to avoid using `eval` for the same reason. Good luck – Day Dec 21 '10 at 17:01
  • i will keep that in mind for all of my javascript to come. heres the result: http://ft.comli.com/ft.v1 (different site because other one wouldnt load) – Tanner Ottinger Dec 21 '10 at 17:26
  • 1
    Thanks for the hint with the browser. Firefox was much clearer in what the real problem was. – ironmouse Feb 28 '19 at 14:49
2

You have endless loop in place:

function save() {
    var filename = id('filename').value;
    var name = id('name').value;
    var text = id('text').value;
    save(filename, name, text);
}

No idea what you're trying to accomplish with that endless loop but first of all get rid of it and see if things are working.

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208