• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 158
  • Last Modified:

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.
0
ThosRM
Asked:
ThosRM
  • 4
  • 2
1 Solution
 
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
 
ThosRMAuthor 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
 
AdamSenior 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
ThosRMAuthor Commented:
Thanks.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now