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.
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.
Share Improve this question edited Mar 19, 2012 at 17:06 BoltClock 725k165 gold badges1.4k silver badges1.4k bronze badges asked Mar 18, 2012 at 23:38 Jesse FultonJesse Fulton 2,2181 gold badge24 silver badges28 bronze badges1 Answer
Reset to default 11Based 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.