How to get “raw” href contents in JavaScript
Try the getAttribute method instead.
Try the getAttribute method instead.
2016 update It’s been over 4 years since this question was posted and things progressed quite a bit. You can’t use: var els = document.getElementsByTagName(“a[href=”http://domain.example”]”); but what you can use is: var els = document.querySelectorAll(“a[href=”http://domain.example”]”); (Note: see below for browser support) which would make the code from your question work exactly as you expect: for … Read more
<a href=”#!” class=”service”>Open</a>
You can do it like this: this is your href: http://maps.google.com/?q= everything that goes after q will be filled in the search filled and submitted, for example: 1200 Pennsylvania Ave SE, Washington, District of Columbia, 20003 So the link looks like this: <a href=”http://maps.google.com/?q=1200 Pennsylvania Ave SE, Washington, District of Columbia, 20003″>…</a> If you need … Read more
I found a solution on this site: using-base-href-with-anchors that doesn’t require jQuery, and here is a working snippet: <base href=”https://example.com/”> <a href=”http://stackoverflow.com/test”>/test</a> <a href=”javascript:;” onclick=”document.location.hash=”test”;”>Anchor</a> Or without inline JavaScript, something like this: document.addEventListener(‘DOMContentLoaded’, function(){ var es = document.getElementsByTagName(‘a’) for(var i=0; i<es.length; i++){ es[i].addEventListener(‘click’, function(e) { e.preventDefault() document.location.hash = e.target.getAttribute(‘href’) }) } })
You need to open a new window: window.open(url); https://developer.mozilla.org/en-US/docs/DOM/window.open
Add a style with the attribute text-decoration:none;: There are a number of different ways of doing this. Inline style: <a href=”https://stackoverflow.com/questions/12528316/xxx.html” style=”text-decoration:none;”>goto this link</a> Inline stylesheet: <html> <head> <style type=”text/css”> a { text-decoration:none; } </style> </head> <body> <a href=”https://stackoverflow.com/questions/12528316/xxx.html”>goto this link</a> </body> </html> External stylesheet: <html> <head> <link rel=”Stylesheet” href=”https://stackoverflow.com/questions/12528316/stylesheet.css” /> </head> <body> <a href=”https://stackoverflow.com/questions/12528316/xxx.html”>goto … Read more
I know this post is old but it still shows at the top of results so I wanted to offer another approach. I see all the regex checks on an anchor element, but why not just use window.location.host and check against the element’s host property? function link_is_external(link_element) { return (link_element.host !== window.location.host); } With jQuery: … Read more
I was looking for just the same thing, and it seems like this will be a feature of HTML5. The attribute you are looking for is rel=”noreferrer”. It is already implemented in Webkit (Chrome, etc.), as well as Firefox, but your mileage may vary. As of 2020, it is supported in all major browsers, with … Read more
I quite enjoy Matt Kruse’s Javascript Best Practices article. In it, he states that using the href section to execute JavaScript code is a bad idea. Even though you have stated that your users must have JavaScript enabled, there’s no reason you can’t have a simple HTML page that all your JavaScript links can point … Read more