Ad

Javascript Filter Over HTMLCollection

I want to get particular query params from all the js files which have been loaded by the browser. My query params reside in the js file named script.js(ex: https://abcd.com/script.js?code=123). In the browser console, I tried like this:

const scripts = document.getElementsByTagName("SCRIPT")
scripts.filter((script) => { return script.src.includes("script.js?code=")})

But this returns the error Uncaught TypeError: document.getElementsByTagName(...).filter is not a function. Please suggest the code changes or is there any better way to get the same?

Ad

Answer

Methods like getElementsByTagName do not return an array rather a list of node. Just wrap your queryselector with brackets and the spread operator to create an array from the nodelist.

const scripts = [...document.getElementsByTagName("SCRIPT")]
scripts.filter((script) => { return script.src.includes("script.js?code=")})

Ad
source: stackoverflow.com
Ad