0

I'm working on new plugin in javascript that load an HTML page using an Ajax request, extract all the scripts from the page and then execute it once all the content has been loaded. To do this I'm trying something like that :

var scripts = '',
    domResponse = $('<div/>').append(HTMLresponse
        .replace(/<\s*script\s*([^>]*)>((.|\n)*)<\s*\/\s*script>/i, function($0,$1,$2){
            scripts += $2;
            return '';
        }));
// Then I load the content and I execute scripts

When I try with a page containing a single script tag it works fine, but if I try with a page like :

<script>
   // Some javascript
</script>

<!-- SOME HTML -->

<script>
   // Another script
</script>

domResponse is empty and scripts contain the text between the first <script> and the last </script>.

Is there any solution to make it work properly ?

2 Answers 2

3

If I understand what you're attempting to do, would this work?

var scriptElements = document.getElementsByTagName("script");
var scripts = "";

for(var i = 0; len = scriptElements.length; i < len; i++) {
    scripts += scriptElements[i].innerHTML;
    scriptElements[i].innerHTML = "";
}

// load content and execute scripts
Sign up to request clarification or add additional context in comments.

2 Comments

+1 for removing the regex for this simple task. TiuShu - this is how i would recommend it...but if you want to use regex, then the problem is that you aren't using 'g' - global directive, if you call it so.
Actually I want to extract script tags from a string, not from the document. First I tried with jQuery "$('<div/>').append(HTMLresponse).find('script');" but jQuery executes scripts when calling "append". That's why I used regex. But maybe there's a workaround using pure javascript ?
0

Like others, I'd recommend against using regex for anything HTML-related.

However, ignoring that, I can still answer your question. Your problem is that you are using a greedy quantifier, i.e. (.|\n)*, which "eats" as much as it can, as long as it ends with </script>. What you want is a non-greedy quantifier, like this:

<\s*script\s*([^>]*)>((.|\n)*?)<\s*\/\s*script>

See here: http://rubular.com/r/U2vvOW6XfZ.

Note that the regex will break if any attribute in a script tag contains a >; if the script for some reason includes a </script> within it (perhaps in a comment); if the page, in general, has commented out a script; etc. This is why it's much better to use a parser.

2 Comments

@ElliotBonneville - I'm voting your answer up; it's better.
Great ! It works ! Thank you for your help ! But you're right, I'll try to find a way to do this without regex...

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.