Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using colons in querySelectorAll()

I'm trying to use the querySelectorAll() method to grab links in web pages, but I want to ignore links that begin with "javascript:" or use another protocol like "itpc://"

Is there any way to include these in a "not()" pseudo selector?

document.querySelectorAll("a:not([href^=javascript]):not([href^=itpc])"); //works
document.querySelectorAll("a:not([href^=javascript:]):not([href^=itpc://])"); //doesn't work

Even though the first method works fine on the current page, there's no guarantee that it will work on every page I'll be using it on, so I'd really like to be able to detect that colon.

like image 765
Jesse Fulton Avatar asked Dec 21 '22 00:12

Jesse Fulton


1 Answers

Based on the spec, turning the values you want to target into strings will work:

document.querySelectorAll("a:not([href^='javascript:']):not([href^='itpc://'])");

The problem with your current version is that unless you use quotes the values have to conform to the restrictions placed on identifiers, which they do not.

like image 126
Jon Avatar answered Jan 08 '23 01:01

Jon