0

I have this function:

function parseScript(_source) {
    var source = _source;
    var scripts = new Array();

    while(source.indexOf("<script") > -1 || source.indexOf("</script") > -1) {
        var s = source.indexOf("<script");
        var s_e = source.indexOf(">", s);
        var e = source.indexOf("</script", s);
        var e_e = source.indexOf(">", e);

        scripts.push(source.substring(s_e+1, e));
        source = source.substring(0, s) + source.substring(e_e+1);
    }
    for(var i=0; i<scripts.length; i++) {
        try {
            eval(scripts[i]);
        }
        catch(ex) {
        }
    }
    return source;
}

It parses and execute Javascript wonderfully except the when in <script type='text/javascript' src='scripts/gen_validatorv31.js'></script> the src file never gets executed.

10
  • 3
    Why on earth you need this, why don't you just let browser parse scripts? Commented Dec 14, 2012 at 18:40
  • I won't get into using eval is a good idea or not. But how are you calling your function? It won't execute on its own. Commented Dec 14, 2012 at 18:41
  • 1
    Like @Teemu said, why are you doing this? What is wrong with browser doing it? Commented Dec 14, 2012 at 18:42
  • @bits I load the content dynamically with AJAX Commented Dec 14, 2012 at 18:50
  • 3
    If I understand your question correctly, the problem is that the parser only works on inline javascript - it can't cope with external files which you would have to load instead using XMLHttpRequest. (And this is probably a bad idea in most cases) Commented Dec 14, 2012 at 18:54

1 Answer 1

1

The parser can only evaluate inline scripts in the file you have opened. To evaluate external scripts you would have to find their sources, probably using something like:

var scripts = source.match(/<script[^>]*src=[^>]*>/g);
if (scripts) {
    for (var i = 0; i < scripts.length; i++) {
        src = scripts[i].match(/src=("([^"]*)"|'([^']*)')/);
        src = src[2] || src[3];
        if (src) {
            addScriptTag(src);
        }
    }
}
else console.log('no external scripts found');

where addScriptTag is described in this answer. addScriptTag adds the script to the head, if possible. It will need to be adapted if you need to add script to the body.

However... why do this? It is slow and messy to parse an entire HTML/Javascript page to get the scripts; for instance you might end up loading the same scripts twice or loading two scripts that don't work well together. Also the scripts may not work if inserted at a different point in the head or body. With AJAX you should only be loading the particular elements you need. Usually this means loading bits of data or HTML to be added to the page. If you have long scripts that are not needed at the beginning but might be needed later then it might be justified to dynamically add new scripts to the page. But in many cases probably better to load all needed scripts at the beginning. And if you really need to switch pages completely then isn't it better to use the old-fashioned method of linking to another page?

Sign up to request clarification or add additional context in comments.

8 Comments

Uncaught TypeError: Cannot read property '1' of null, Uncaught TypeError: Cannot read property 'length' of null
Uncaught TypeError: Cannot read property 'length' of null. I'm also experimenting now...
ok I tried again. The regex needs to catch single quotes as well as double.
This seems to skip over one of the Javascript files <script type="text/javascript" src="scripts/pwdwidget.js"></script>
Third time lucky... there was a small error I corrected (src = scripts[0] should have been src = scripts[i])
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.