richardstelmach
richardstelmach

Reputation: 373

Unexpected Token: ILLEGAL on basic click function, jQuery

I'm just doing a very simple click function but I get an error on line 6. Even I remove everything other than the click function - still an error. The object is definitely in the HTML and I have alerted $("p#backButton") to check.

$(document).ready(function() {

    var windowWidth = $(document).width();
    if(windowWidth < 767){
        $("p#backButton").css("display","block");
        $("p#backButton a").click(function(e) {
            e.preventDefault();
            history.back();
            return false;
        });
     }
});

Upvotes: 0

Views: 743

Answers (2)

richardstelmach
richardstelmach

Reputation: 373

adding:

<a href="#back" onclick="history.back(); return false;" title="Go Back">Back</a>

As inline javascript was a work around for this issue - not a desirable fix but was the only way I found to fix it.

Upvotes: 0

OneChillDude
OneChillDude

Reputation: 8006

I think your issue stems from using history.back() which in that scope may be undefined. Try using window.history.back()

Also try spacing out your code a little bit

$(document).ready(function() {

    var windowWidth = $(document).width();
    if ( windowWidth < 767 ) {
        $("p#backButton").css("display","block");
        $("p#backButton a").click( function(e) {
            e.preventDefault();
            window.history.back();
            return false;
        });
     }
});

Good luck. Let me know what happens

Upvotes: 1

Related Questions