How can I fix this error "missing; before statement" in javascript?

31,444

Solution 1

When assigning a function to a variable, you need a semicolon after the function.

Example: var func = function() { return false; };

Solution 2

Put a semicolon after all statements. JavaScript does it automatically for you when you "forget" one at the end of a line**, but since you used a tool to make everything fit on one line, this doesn't happen anymore.

** it should also be happening when a statement is followed by a }, but it's just bad practice to rely on it. I would always write all semicolons myself.

Actually, you know what, since it's so easy, I did it for you:

function getSelected() {
    var selText;
    var iframeWindow = window;
    if (iframeWindow.getSelection) {
        selText = iframeWindow.getSelection() + "";
    } else if (iframeWindow.document.selection) {
        selText = iframeWindow.document.selection.createRange().text;
    }
    selText = $.trim(selText);
    if (selText != "") {
        return selText;
    } else {
        return null;
    }
}
$(document).ready(function () {
    function scan_selectedText() {
        if (getSelected() == null) {
            return false;

        }
        if (getSelected().length < 25) {
            return false;
        }
        $(document)[0].oncontextmenu = function () {
            return false;
        };
        var result = true;
        var selected_Text = getSelected();
        selected_Text = selected_Text.replace(/ {2,}/g, ' ').replace(/\s{2,}/g, ' ');
        $('#content .para').each(function () {
            var accepted_text = $.trim($(this).text());
            accepted_text = accepted_text.replace(/ {2,}/g, ' ').replace(/\s{2,}/g, ' ');
            if (accepted_text.search(selected_Text) > -1) {
                result = false;
            }
        });
        var AllAccepted = "";
        $('#content .para').each(function () {
            var correntDiv = $.trim($(this).text()).replace(/ {2,}/g, ' ').replace(/\s{2,}/g, ' ');
            AllAccepted = AllAccepted + correntDiv + " ";
        });
        if ($.trim(AllAccepted).search(selected_Text) > -1) {
            return false;
        }
        if (!result) {
            return false;
        }
        var body = $.trim($('body').text());
        body = body.replace(/ {2,}/g, ' ').replace(/\s{2,}/g, ' ');
        var bodyWithoutDivs = body;
        $('#content').each(function () {
            var correntDiv = new RegExp($.trim($(this).text()).replace(/ {2,}/g, ' ').replace(/\s{2,}/g, ' '), "");
            bodyWithoutDivs = bodyWithoutDivs.replace(correntDiv, '');
        });
        if (bodyWithoutDivs.search(selected_Text) > -1) {
            return false;
        }
        if (body == selected_Text) {
            return true;
        }
        return true;
    }
    $(document).mousedown(function (key) {
        if (key.button == 2) {
            if (scan_selectedText() == true) {
                $(document)[0].oncontextmenu = function () {
                    return false;
                };
            } else {
                $(document)[0].oncontextmenu = function () {
                    return true;
                };
            }
        }
    });
    var isCtrl = false;
    $(document).keyup(function (e) {
        if (e.which == 17) isCtrl = false;
    }).keydown(function (e) {
        if (e.which == 17) isCtrl = true;
        if (e.which == 67 && isCtrl == true) {
            $("#content2").animate({
                opacity: 0
            }, 500).animate({
                opacity: 1
            }, 500);
            if (scan_selectedText() == true) {
                return false;
            } else {
                return true;
            }
        }
    });
    document.onkeypress = function (evt) {
        if (evt.ctrlKey == true && evt.keyCode == 67) {
            $("#content2").animate({
                opacity: 0
            }, 500).animate({
                opacity: 1
            }, 500);
            if (scan_selectedText() == true) {
                return false;
            } else {
                return true;
            }
        }
    };
    $('*').bind('copy', function (key) {
        if (scan_selectedText() == true) {
            return false;
        } else {
            return true;
        }
    });
});

Solution 3

First thing, start with the raw human-written version of your javascript, you know, the unminimized non-machine-generated version

After you've fixed you've made sure it is free from syntax errors .... and you minimize it, don't make a mistake when copy/pasting

Share:
31,444
faressoft
Author by

faressoft

Updated on July 25, 2022

Comments