0

I want to match any raw script in an Ajax downloaded document, so I tried

$.ajax({
    url: url,
    type: "post",
    success: function (data, status, xhr) {
        var scr = $(data).find('script[type="text/javascript"]');

The call is returning sucess, but the selector is not returning a match i.e. 'script[type="text/javascript"]' has a length of 0.

The page being loaded (i.e. data) definitely contains a test script tag like this:

    <script type="text/javascript">
        $(function () {
            alert("JS running");
        });
    </script>

What am I missing here? Is it the way JQuery parses raw HTML?

Followup:

this also returns no matches: var scr = data.find('script');

Note: Looking at the contents of $(data) it appears the JQuery parser strips out any Javascript tags.

iCollect.it Ltd
  • 92,391
  • 25
  • 181
  • 202
  • @Samuel Liew: Question updated. That also returns no matches... The plot thickens. $.getScript() is I gather only useful for downloading JScript files. This code is embedded in a dynamically loaded form page so I need access to any script in the page. Thanks – iCollect.it Ltd Apr 23 '13 at 08:00
  • one possible solution is to set `datatype: "text"` – Arun P Johny Apr 23 '13 at 08:19
  • otherwise, why don't to use `load()` instead of `$.ajax()` – Arun P Johny Apr 23 '13 at 08:21
  • @Arun P Johny: I believe `load` won't do a `post`, only a `get` (I am dynamically loading forms into panels and reconnecting the JS). – iCollect.it Ltd Apr 23 '13 at 08:36

2 Answers2

3

You need to use regex here. Use Javascript .match.

Also, the string you comparing script[type="text/javascript"] is different than the one in the var: <script type="text/javascript">.

You should use it like:

matched = data.match('<script .*</script>');

It will have what was matched.

iCollect.it Ltd
  • 92,391
  • 25
  • 181
  • 202
kailash19
  • 1,771
  • 3
  • 22
  • 39
  • What do you mean different? It is a standard JQuery selector where the tag is `script` and the type of the tag is equal to `text/javascript`. – iCollect.it Ltd Apr 23 '13 at 07:53
  • oh sorry i misinterpreted it..i was in impression you just comparing them. – kailash19 Apr 23 '13 at 07:59
  • It's a JQuery question to do with JQuery selector. Thanks anyway. Your example regex (regex might be a fallback if I can't get the selector to work), would not match script contents, just the opening script tags. – iCollect.it Ltd Apr 23 '13 at 08:03
  • +1 for suggesting regex, but I needed a more robust match. I have completely the puzzle in my own answer. Thanks – iCollect.it Ltd Apr 23 '13 at 08:38
0

It appears that $(htmlstring) strips any script tags from the HTML. That is a shame.

The solution I came up with is based on the regex in the answer to How to get regex to match multiple script tags?

        var re = /<script\b[^>]*>([\s\S]*?)<\/script>/gm;
        var scripts = "";
        var match;
        while (match = re.exec(data)) {
            if (match[1] != "") {
                scripts += match[0];
            }
        }
        panel.append(scripts);

I concatenate any inline scripts and the script executes immediately upon being appended to the DOM.

Community
  • 1
  • 1
iCollect.it Ltd
  • 92,391
  • 25
  • 181
  • 202