Is it possible to use javascript to add target="_blank" to all links that include the following in the URL: profile.php?id=
Is it possible to use javascript to add target=_blank to all links that include
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The following should work:
JS Fiddle demo.
Edited to improve the performance of the above code, removing
document.linksand replacing it withdocument.getElementsByTagName('a'):JS Fiddle demo.
It’s worth noting, though, that using Chris Fulstow‘s
querySelectorAll()-enabled$('a[href*="profile.php?id="]')approach is marginally faster (in Chromium 14/Ubuntu 11.04): JS Perf speed-test. Therefore, the following is the fastest (in browsers that supportquerySelectorAll()):JS Fiddle demo.
Above assertion ‘fastest’ supported by JS Perf comparison, at least in Chromium 14/Ubuntu 11.04. Bear in mind, of course, that IE < 8, and Firefox 3, aren’t going to play well with this approach.
Of course, the above should be corrected to:
JS Fiddle demo.
This is because the
ifcondition was already evaluated byquerySelectorAll(), which makes theifentirely redundant.Modified JS Perf comparison: http://jsperf.com/anchor-selector-test/3.