wordSmith
wordSmith

Reputation: 3183

unexpected token error when comparing two strings

I'm going through the existing scripts on the page and comparing them to one i'm about to load to see if it is is already loaded. If it is loaded then I will call a function that uses it. If not then I will load it and call a function that is using it.

However, I'm getting an unexpected token error whether I use == or === for the comparison of the existing script tag's attribute to the src value I want to add.

What am I doing wrong that is causing the error?

function isScriptAlreadyIncluded(src){
    var scripts = document.getElementsByTagName("script");
    for(var i = 0; i < scripts.length; i++){
        if(scripts[i].getAttribute('src')) == src) return true;
        return false;
    }
}
if(isScriptAlreadyIncluded('contextualConversation.js')) contextualReplace();
else{
    var cCScript = document.createElement('script');
    cCScript.src = 'contextualConversation.js';
    contextualReplace();
}

Upvotes: 2

Views: 310

Answers (1)

imsky
imsky

Reputation: 3279

You have an extra paren in your comparison line.

Updated:

if(scripts[i].getAttribute('src') == src) return true;

Original:

if(scripts[i].getAttribute('src')) == src) return true;

Upvotes: 2

Related Questions