Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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=")})

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading