Magnus
Magnus

Reputation: 424

External JS file causes 'Unexpected token <' on one page but not on another

Hey so I started getting this error Unexpected token < today and I've been trying to locate the error and it seems to come from my main.js file when I'm on the page edit.php. When I'm on dashboard.php it works great with no errors! I've ran the code through multiple JS lint softwares with no luck. I've included a snippet of the code I recently added.

function navigateTo(action, path)
{
    var id = $(event.currentTarget).attr('id');
    var f_path = $.parseJSON($("body").data("firebase_path"));

    if(action === 0)
    {
        firebase = firebase.root();
        firebase = firebase.child(f_path);

        firebase.off("child_added", fb_event_added);
        firebase.off("child_changed", fb_event_changed);

        $("#files").empty();
        hideDetails();

        fb_event_added = firebase.orderByPriority().on('child_added', function(push)
        {
            triggerFirebaseEvents("file", "child_added", push);
        });
        fb_event_changed = firebase.orderByPriority().on('child_changed', function(push)
        {
            triggerFirebaseEvents("file", "child_changed", push);
        });
    }
}

function triggerFirebaseEvents(type, event, push)
{
    var key = push.key(), icon_type;
    push = push.val();

    if(type == "file" && event == "child_added")
    {
        $('<div id="' + key + '" class="col-xs-6 col-md-3 file">').append(
            $('<div id="file_' + key + '" class="well well-sm">').html('<p class="mn"><i class="fa ' + icon_type + ' fa-fw text-' + push.color + '"></i> ' + push.name + '</p>')
        ).appendTo('#files');

        $("#file_" + key).data('file-details', JSON.stringify(push));
        UIkit.sortable($(".file"), { });
    }
    else if(type == "file" && event == "child_changed")
    {
        if(push.removed) $("#" + key).addClass("hidden");
        else
        {
            $('#' + key).empty().append(
                $('<div id="file_' + key + '" class="well well-sm">').html('<p class="mn"><i class="fa ' + icon_type + ' fa-fw text-' + push.color + '"></i> ' + push.name + '</p>')
            );
            $("#file_" + key).data('file-details', JSON.stringify(push));
        }
    }
}

It's weird that it works on one page but not the other.

PS. Could it have something to do with a poorly configured .htaccess maybe? Because on the file it throws the error, there's a htaccess rewrite configured.

Upvotes: 2

Views: 1270

Answers (1)

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167192

That's because the JavaScript file which you are trying to load has either:

  • Unescaped HTML content.
  • Loads a 404 or other Error page.

Make sure you give the URLs correctly and handle them too. Checking in the Network tab, shows what exactly happens when you request the URL.

Upvotes: 4

Related Questions