What does this javascript function do?

I do not know javascript and would like to know what function(path) is trying to accomplish. It produces a syntax error at the underscore on the 4th line.

(function () {
      var b = bootstrap,
        b.includeScript = function(path) {
            path = path.replace(../_.js$/, "");
            var tag = (path.match(/-(.+)/) || ["", ""])[1];
            if ( (tag in b.tags) && !(b.tags[tag]) ) return;
            document.write('<script src="' + b.js" type="text/javascript" charset="utf-8"><\/script>');
      };
})();

Thanks.
ThomasAsked:
Who is Participating?
 
AdamConnect With a Mentor Senior DeveloperCommented:
$ in a regular expression is the end of a line. There should not be anything after .jswhen it is passed to the regex.

The extra quote ' after b.js is certainly needed!

Sometimes, when you have <script inside a script (as you have here) it can cause issues.
If it does, you can break "<script src=" into "<scr"+"ipt src=" so the extra <script is not picked up when the parent script is being executed
0
 
AdamSenior DeveloperCommented:
The syntax error in line 4 is probably because the regular expression is malformed
path = path.replace(../_.js$/, "");

Open in new window

The regular expression should start and finish with a /
The .. in front of it would cause an error.

Chang it to
path = path.replace(/_.js$/, "");

Open in new window

to fix it
All this line does is remove _.js from the end of path

The next line returns everything after the first - in path

i.e. if the original 'path' = abc-def_.js then tag = def
If there is no -, then tag is an empty string

b is an object. it would appear to have at least 2 properties .tag and .js
b.tag would appear to be an array

The line
 if ( (tag in b.tags) && !(b.tags[tag]) ) return;

Open in new window

first looks to see if the string in the 'tag' variable is property of the b.tags, but not a key in the array b.tags If this matches, the function ends.
If it doesn't match, then a new script tag is written to the page, with a source of b.js

0
 
ThomasAuthor Commented:
I apologize for the delay in responding.

I changed ../_.js$/ to /_.js$/ as Cyber-spy suggested and that took care of the syntax error.

Cyber-spy, what is the $ in .js$

The following line also had a syntax error, so I changed:
document.write('<script src="' + b.js" type="text/javascript" charset="utf-8"><\/script>');
to
document.write('<script src="' + b.js + '" type="text/javascript" charset="utf-8"><\/script>');
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
AdamSenior DeveloperCommented:
Here's a useful Regex reference:

http://www.regextester.com/jssyntax.html

Open in new window

0
 
AdamSenior DeveloperCommented:
Whoops - I selected 'code' instead of a link! Doh!
0
 
ThomasAuthor Commented:
Thanks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.