最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Extract src attribute from script tag and parse according to particular matches - Stack Overflow

programmeradmin5浏览0评论

So, I have to determine page type in a proprietary CRM, using JavaScript. The only way to determine the page type (that is, the only consistent difference on the front end) is by examining a script tag (out of many list) whose src attribute begins with /modules/.

In a list of a dozen or so script tags in the header, each page has a line of the following format

 <script src="/modules/example/includes/sample.js" type="text/javascript"></script> 

Now, the order of the script tag is never the same, but, there's always one script that has /modules/blah. I need to extract blah to my script can detect what kind of page it is.

So, how do I, using either JavaScript or jQuery, extract the script tag's src value, where src begins with /modules, and store the value after that ('example', in the example above) as a javascript variable?

So, I have to determine page type in a proprietary CRM, using JavaScript. The only way to determine the page type (that is, the only consistent difference on the front end) is by examining a script tag (out of many list) whose src attribute begins with /modules/.

In a list of a dozen or so script tags in the header, each page has a line of the following format

 <script src="/modules/example/includes/sample.js" type="text/javascript"></script> 

Now, the order of the script tag is never the same, but, there's always one script that has /modules/blah. I need to extract blah to my script can detect what kind of page it is.

So, how do I, using either JavaScript or jQuery, extract the script tag's src value, where src begins with /modules, and store the value after that ('example', in the example above) as a javascript variable?

Share Improve this question asked Jul 8, 2010 at 16:51 YahelYahel 37.3k23 gold badges106 silver badges154 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 20

Well, you can start by collecting all of the script elements. With jQuery, that's as simple as

var scripts = $("script");

Then limit that set to the elements that have a src attribute:

var scripts = $("script[src]");

...and further limit it to those with a src attribute beginning with "/modules/":

var scripts = $("script[src^='/modules/']");

...which given your description should result in a set of exactly one element, from which you can now pull the src attribute value itself:

var path = $("script[src^='/modules/']").attr('src');

Ok, that was easy - now to extract the next part of the path. There are plenty of ways to do this, but split is quick & dumb: create an array of parts using '/' as the separator, then pick off the third element (which will be the one after "modules"):

var pathPart = $("script[src^='/modules/']").attr('src').split('/')[2];

Obviously, this is all very specific to the exact format of the script path you're using as an example, but it should give you a good idea of how to begin...

发布评论

评论列表(0)

  1. 暂无评论