0

In php how would I grab all javascript from a page given it's url? Is there a good regular expression to get the src of all javascript script tags or the script inside of them?

2
  • Javascript code is usually bound to the page they're into (excluding common libraries/frameworks) so they're pretty much useless without the original page and its backend, e.g. you can't grab Facebook's JS and put it in your page to get the chat bar on the bottom Commented May 31, 2010 at 21:11
  • I'm building a jsLint checker, just going to check the js for syntax errors Commented May 31, 2010 at 22:25

3 Answers 3

1

You can use PHP Simple HTML DOM to traverse the DOM for <script> tags. You can grab inline scripts directly in a string and get the src attribute for externally linked scripts and download them directly with curl or something. It would require some coding, I don't know if there is a 'magic' script that would do that automatically for you.

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

3 Comments

Don't forget javascript: URIs and intrinsic event handler attributes (such as onclick).
would php simple html dom work even if the site doesn't have a valid dom structure, (like most sites)?
@QuinnBaetz: I guess it would in most cases. You need to try it to be certain, but where I've used it I didn't ran into any issues. It's very easy to use also.
0

This should place the values of all src attributes contained in script tags into an array in the variable $matches. Check out the documentation for the format of the the array, as there is another parameter that will allow you to modify it.

preg_match_all('/<script[^>]*src=[\'"]([^\'"])+[\'"]/', $string, $matches);

Comments

0

I would suggest htmlSQL.

http://www.jonasjohn.de/lab/htmlsql.htm

With that you can get the code with tags as well as inline javascript for onclick like events also.

Comments

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.