Get link tag content using javascript - not CSS

Assuming I have

<LINK rel="Index" href="index.html"> <LINK rel="Next" href="Chapter3.html"> <LINK rel="Prev" href="Chapter1.html"> 

(taken from an example w3 website)

Does anyone know if they are accessible via DOM JavaScript?

I want to know. If I have link tags like this in an HTML document, are they read as the main document and added to the DOM, and if I can also access their DOM.

+5
javascript dom
Jul 29 '12 at 10:09
source share
2 answers

I have this code:

 <script type="text/javascript"> var your_url = 'http://www.example.com'; </script> <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js" ></script> <script type="text/javascript"> // jquery.xdomainajax.js ------ from padolsey jQuery.ajax = (function(_ajax){ var protocol = location.protocol, hostname = location.hostname, exRegex = RegExp(protocol + '//' + hostname), YQL = 'http' + (/^https/.test(protocol)?'s':'') + '://query.yahooapis.com/v1/public/yql?callback=?', query = 'select * from html where url="{URL}" and xpath="*"'; function isExternal(url) { return !exRegex.test(url) && /:\/\//.test(url); } return function(o) { var url = o.url; if ( /get/i.test(o.type) && !/json/i.test(o.dataType) && isExternal(url) ) { // Manipulate options so that JSONP-x request is made to YQL o.url = YQL; o.dataType = 'json'; o.data = { q: query.replace( '{URL}', url + (o.data ? (/\?/.test(url) ? '&' : '?') + jQuery.param(o.data) : '') ), format: 'xml' }; // Since it a JSONP request // complete === success if (!o.success && o.complete) { o.success = o.complete; delete o.complete; } o.success = (function(_success){ return function(data) { if (_success) { // Fake XHR callback. _success.call(this, { responseText: data.results[0] // YQL screws with <script>s // Get rid of them .replace(/<script[^>]+?\/>|<script(.|\s)*?\/script>/gi, '') }, 'success'); } }; })(o.success); } return _ajax.apply(this, arguments); }; })(jQuery.ajax); $.ajax({ url: your_url, type: 'GET', success: function(res) { var text = res.responseText; // then you can manipulate your text as you wish alert(text); } }); </script> 
+11
Aug 26 '13 at 15:26
source share

Try this: http://jsfiddle.net/TpTsJ/ (the first 2 are JSFiddle links - they will not be on your page)

 var links = document.getElementsByTagName("link"); for ( var i = 0; i < links.length; i++ ) { alert( links[i].getAttribute("rel") + ' : ' + links[i].getAttribute("href") ); }​ 
+2
Jul 29 '12 at 10:17
source share



All Articles