Set Only Links Within Specific DIV to Different Frame Target (e.g. New Tab/Window)

Mindwatering Incorporated

Author: Tripp W Black

Created: 06/14/2011 at 01:44 AM

 

Category:
Notes Developer Tips
JavaScript, Views

Issue:
Have a web page whose links open in same window. However, you want all links within a specific part of the page to load into a new tab/window. (For example, the search results from a search)

Solution:
You could do a page-wide default and then have the logic that spits out the results include it, but hard coding that does scale well or adapt to web side GUI changes. Solution, is to do it within the GUI since the "problem" is in the front-side GUI.
Here is a JavaScript solution:

a. Around the search results, add a new DIV like so:
<div id="searchviewbody" name="searchviewbody">$$ViewBody</div>

b. Then add the following to the JSHeader:
function SearchLinksTarget()
{
// get search div
searchDiv = document.getElementById('searchviewbody');
// get the div links
divlinks = searchDiv .getElementsByTagName('a');
// Loop through those links and attach the target attribute
for (var i=0, len=divlinks.length; i < len; i++) {
// the _blank will make the link open in new window
divlinks[i].setAttribute('target', '_blank');
}
}


c: Then add the following to the onLoad event:
SearchLinksTarget();



previous page