0
document.getElementById("but").onclick = function(e) {
showDropDown(this, e);
};

function showDropDown(element, e) {
element.onclick = function() {};
if (e.stopPropagation) e.stopPropagation(); // W3C model
else e.cancelBubble = true; // IE model
document.getElementById("window").style.display = "inline-block";
document.onclick = function(e) {
    var ele = document.elementFromPoint(e.clientX, e.clientY);
    if (ele == element) {
        hideDropDown();
        return;
    }
    do {
        if (ele == document.getElementById("window")) return;
    } while (ele = ele.parentNode);
    hideDropDown(element);
};
}

function hideDropDown(element) {
document.onclick = function() {};
document.getElementById("window").style.display = "none";
element.onclick = function(e) {
    showDropDown(this, e);
};
}​


   <input id="but" type="button" value="pressMe" />
   <div id="window" style="display:none">popup</div>​

errors: https://www.dropbox.com/s/uzeiq6043rvueqf/Capture.PNG https://www.dropbox.com/s/w3rct18cumwva7m/bar3.png

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Sami Al-Subhi
  • 4,406
  • 9
  • 39
  • 66
  • 1
    It looks like you're missing the tag!! Just put it above your tag. – frenchie Aug 26 '12 at 05:58
  • I took a look at it, and exactly what I said: You have a hidden illegal character. You might want to turn on 'show hidden characters' in your editor, and see if you can remove the hidden character. – Lucas Aug 26 '12 at 06:13
  • I agree with @think123 http://stackoverflow.com/questions/4404526/unexpected-token-illegal-in-webkit – mask8 Aug 26 '12 at 06:18
  • That's right. I copied from jsfiddle. I did not know that. Thanks for sharing this :). – Sami Al-Subhi Aug 26 '12 at 06:20
  • @SamiAl-Subhi If you think my answer is good, then can you please tick my one as correct? – Lucas Aug 26 '12 at 06:41
  • duplicate: http://stackoverflow.com/questions/4404526/unexpected-token-illegal-in-webkit – Lucas Aug 26 '12 at 06:42

3 Answers3

1

You have error because your document is not loaded.
put your code in window.onload:

window.onload=function(){
  //code
}

or if you are using jquery:

$(document).ready(function(){
  //code
});
  • I don't think it matters for syntax errors, does it? – mask8 Aug 26 '12 at 06:11
  • that would not be part of a `Unexpected token ILLEGAL` error. I cannot exactly remember that type of error, but it has something to do with 'null', as the element does not exist yet. – Lucas Aug 26 '12 at 06:11
0
document.getElementById("but").onclick = function(e) {
    showDropDown(this, e);
};

function showDropDown(element, e) {
    element.onclick = function() {};
    if (e.stopPropagation) 
        e.stopPropagation(); // W3C model
    else 
        e.cancelBubble = true; // IE model
    document.getElementById("window").style.display = "inline-block";
    document.onclick = function(e) 
    {
        var ele = document.elementFromPoint(e.clientX, e.clientY);
        if (ele == element) {
            hideDropDown();
            return;
        }
        do {
            if (ele == document.getElementById("window")) return;
        } while ((ele = ele.parentNode) !== null);
        hideDropDown(element);
    };
}

function hideDropDown(element){
    document.onclick = function() {};
    document.getElementById("window").style.display = "none";
    element.onclick = function(e) {
        showDropDown(this, e);
    };
}
TheHe
  • 2,933
  • 18
  • 22
  • you should at least explain why your code works. – Lucas Aug 26 '12 at 06:43
  • in the while-condition, there was no bool-check (strict-error) (in the document.onclick-function) and the hidden character which is pointed out by musa... – TheHe Aug 26 '12 at 07:09
0

What you did was where you copied or wrote this code, there most likely was a bug in it. JSFiddle has been known to have such problems. What you have to do is type that section of code (1 line above the error and 1 line underneath it) in a simple editor such as Notepad or TextEdit, and then copy that over and replace your current code. I know its this error, as the Unexoected token ILLEGAL part of it meant that the hidden character which is placed there is obviously not JavaScript-compliant, therefore it is not a syntax error at all.

Works for me.

Lucas
  • 16,930
  • 31
  • 110
  • 182