I know that $("a[href='http://testtesttest.org/']").attr("target", "_blank"); will find and change all links that match the test url, but I’d like to know how to only change the links that do NOT match that URL. Essentially links that would take users to other webites — outside links
I know that $(a[href=’http://testtesttest.org/’]).attr(target, _blank); will find and change all links that match the
Share
You can use
:not:If you want to select all those that don’t start with this URL, you have to use the attribute starts with selector
^=:You mention:
That depends on how consistent the structure of internal URLs is. E.g. the previous shown selector would also select links such as
or
which are clearly internal. If every internal link starts with your domain name, than it is fine. If not, you either have the possibility to adjust those links, to have a consistent structure, or to use another selector:
This will make sure that links with absolute and relative URLs are not selected.
If you also have links with other protocols, such as
file:, it becomes even more involved.