How to find all links to private pages, javascript, jquery -
here's scenario: client request behavior (a lightbox) on click of link private page when site visitor not logged site.
key: private pages on site not listed in top navigation html until user logged in site.
there no parameters or identifiers links going private pages. but can know links public referencing path names of top navigation, thereby ruling out public links , external links, every other link on page must internal & private.
what know:
- the url host (which can use find if link internal or external
- navigation links public (we can find looking @ our top navigation structure)
- external links (we can find based on knowing host url)
is possible know links on site internal & private based on ruling out known public & external links, might way write using jquery or js?
edit: links internal not guaranteed relative, absolute
edit 2.0:
what assume can know...
current url (this needs dynamic script used on multiple sites move production urls w/e client chooses): http://example.com
external url: ? anything, need find or rule out links. don't want target those
this example of navigation looks like.
<div class="navigation"> <a href="http://example.com/home"></a> <a href="http://example.com/about"></a> <a href="http://example.com/register"></a> </div>
i not want target links displayed in top navigation these public. (again, needs dynamic script used on multiple sites move production urls w/e client chooses, , client has capability make pages public need.)
so known urls, href's & links.
what want target links still on http://example.com not listed in navigation
div.
assuming external links absolute , internal links relative, this:
$("a[href^='http']").addclass("external-link");
otherwise, this:
var knownexternalhosts = ["foo.com"]; $("a") .filter(function(){ var found = false; knownexternalhosts.foreach(function(knownhost){ if(~this.href.indexof(knownhost)) found = true; }, this); return found; }) .addclass("external-link") ;
updated answer (based on feedback)
this solution should match links in array.
var knownlinks = ["http://foo.com", "/foo"]; $("a") .filter(function(){ var found = false; knownlinks.foreach(function(knownlink){ if($(this).attr("href") === knownlink) found = true; }, this); return found; }) .addclass("known-link") ;
here fiddle: http://jsfiddle.net/moderndegree/se4kl/
Comments
Post a Comment