XAceX Posted October 24, 2007 Share Posted October 24, 2007 Ok im not very good with JS but I know that there is a function for opening stuff in a new window. which is not always good but I know its not bad when opening PDFs. which is what I need to do for my companies site. How would I go about setting only the links that open PDFS to open in a new window and links for the site or web docs on the site to open in the same window? Link to comment Share on other sites More sharing options...
justsomeguy Posted October 24, 2007 Share Posted October 24, 2007 For any link that you want to open in a new window you can use one of two things. If the site is HTML, then you can put a target on the link:<a href="file.pdf" target="_blank">If the site is XHTML, then you can use window.open.<a href="java script:void(0);" onclick="window.open('file.pdf', '', '');">Check the Javascript reference for window.open to see what options you can use when opening the window. Link to comment Share on other sites More sharing options...
Synook Posted October 24, 2007 Share Posted October 24, 2007 If you have an existing website and want to use JS to make none-PDF hyperlinks not open in new windows, you can put this in your <head> <script type="text/javascript"> window.onload = function() { hyperlinks = document.getElementsByTagName('a'); for (i = 0; i < hyperlinks.length; i++) { hyperlink = hyperlinks[i].href; extension = hyperlink.split(".")[hyperlink.split(".").length - 1] if (extension != "pdf") { document.getElementsByTagName('a')[i].target = ""; } } }</script> Link to comment Share on other sites More sharing options...
XAceX Posted October 25, 2007 Author Share Posted October 25, 2007 Thanks you guys. Both helped a lot the only thing is there is already existing code so I need to know where it goes or what it replaces. Here is what was already created. <html> <head> <title>FGL Customer Services</title> <meta content=""> <LINK href="css/fglweb.css" rel="stylesheet" type="text/css"> <script LANGUAGE="JavaScript" SRC="javascript/menu.js"></SCRIPT> <script type="text/javascript"> startList = function() { if (document.all&&document.getElementById) { navRoot = document.getElementById("nav"); for (i=0; i<navRoot.childNodes.length; i++) { node = navRoot.childNodes[i]; if (node.nodeName=="LI") { node.onmouseover=function() { this.className+=" over"; } node.onmouseout=function() { this.className=this.className.replace(" over", ""); } } } } } window.onload=startList;/************************************************ Contractible Headers script- Dynamic Drive (www.dynamicdrive.com)* This notice must stay intact for legal use. Last updated Mar 23rd, 2004.* Visit [url="http://www.dynamicdrive.com/"]http://www.dynamicdrive.com/[/url] for full source code***********************************************/var enablepersist="on" //Enable saving state of content structure using session cookies? (on/off)var collapseprevious="no" //Collapse previously open content when opening present? (yes/no)if (document.getElementById){document.write('<style type="text/css">')document.write('.switchcontent{display:none;}')document.write('</style>')}function getElementbyClass(classname){ccollect=new Array()var inc=0var alltags=document.all? document.all : document.getElementsByTagName("*")for (i=0; i<alltags.length; i++){if (alltags[i].className==classname)ccollect[inc++]=alltags[i]}}function contractcontent(omit){var inc=0while (ccollect[inc]){if (ccollect[inc].id!=omit)ccollect[inc].style.display="none"inc++}}function expandcontent(cid){if (typeof ccollect!="undefined"){if (collapseprevious=="yes")contractcontent(cid)document.getElementById(cid).style.display=(document.getElementById(cid).style.display!="block")? "block" : "none"}}function revivecontent(){contractcontent("omitnothing")selectedItem=getselectedItem()selectedComponents=selectedItem.split("|")for (i=0; i<selectedComponents.length-1; i++)document.getElementById(selectedComponents[i]).style.display="block"}function get_cookie(Name) { var search = Name + "="var returnvalue = "";if (document.cookie.length > 0) {offset = document.cookie.indexOf(search)if (offset != -1) { offset += search.lengthend = document.cookie.indexOf(";", offset);if (end == -1) end = document.cookie.length;returnvalue=unescape(document.cookie.substring(offset, end))}}return returnvalue;}function getselectedItem(){if (get_cookie(window.location.pathname) != ""){selectedItem=get_cookie(window.location.pathname)return selectedItem}elsereturn ""}function saveswitchstate(){var inc=0, selectedItem=""while (ccollect[inc]){if (ccollect[inc].style.display=="block")selectedItem+=ccollect[inc].id+"|"inc++}document.cookie=window.location.pathname+"="+selectedItem}function do_onload(){uniqueidn=window.location.pathname+"firsttimeload"getElementbyClass("switchcontent")if (enablepersist=="on" && typeof ccollect!="undefined"){document.cookie=(get_cookie(uniqueidn)=="")? uniqueidn+"=1" : uniqueidn+"=0" firsttimeload=(get_cookie(uniqueidn)==1)? 1 : 0 //check if this is 1st page loadif (!firsttimeload)revivecontent()}}(window.addEventListener)window.addEventListener("load", do_onload, false)else if (window.attachEvent)window.attachEvent("onload", do_onload)else if (document.getElementById)window.onload=do_onloadif (enablepersist=="on" && document.getElementById)window.onunload=saveswitchstate</script> </head> <body topmargin=0 leftmargin=0 rightmargin=0 bottommargin=0 bgcolor=#94AE94> <?php $choice=$_GET['choice']; ?> <div align=center> <table width=780 cellpadding=0 cellspacing=1 bgcolor=black> <tr> <td> <table width=778 border=0 cellspacing=0 cellpadding=0 height=550> <tr> <td height=110> <?php include('fgl_library/header.php'); ?> </td> </tr> <tr valign=top> <?php if($choice=="drinking" || $choice=="waste" || $choice=="hazardous"){ $bgcolor="#6384A0"; } else { $bgcolor="#FFFFFF"; } ?> <td align=center bgcolor=#F0F0F0> <br> <table bgcolor=black cellpadding=0 width=700> <tr bgcolor=<?php echo $bgcolor; ?>> <?php echo "<td align=center>"; if(!$choice){ include('customerservices_insert.php'); } elseif($choice=="drinking") { include('customer/drinking.php'); } elseif($choice=="waste") { include('customer/waste.php'); } elseif($choice=="hazardous") { include('customer/hazardous.php'); } elseif($choice=="agronomics") { include('customer/agronomics.php'); } elseif($choice=="environmental") { include('customer/environmental.php'); } elseif($choice=="support") { include('customer/support.php'); } elseif($choice=="references") { include('customer/references.php'); } elseif($choice=="samppro_ag"){ include('customer/SamplingProcedures-AG.php'); } elseif($choice=="samppro_env"){ include('customer/SamplingProcedures-ENV.php'); } elseif($choice=="glossary"){ include('customer/glossary.php'); } elseif($choice=="links"){ include('customer/links.php'); } elseif($choice=="FAQ"){ include('customer/FAQ.php'); } ?> </td> </tr> </table> <br> <?php if($choice=="environmental" || $choice=="agronomics"){ $Contact=ucfirst($choice); echo "<font face=arial size=-1>For More $Contact Information Contact:<br>"; if($choice=="environmental"){ echo "<b>Vickie Taylor</b> Phone: (805)392-2010 <a href=mailto:vickiet@fglinc.com class=econtact>vickiet@fglinc.com</a><br>"; } elseif($choice=="agronomics") { echo "<b>Ellie Blackshear</b> Phone: (805)392-2020 <a href=mailto:ellieb@fglinc.com class=econtact>ellieb@fglinc.com</a></font><br>"; } echo "<br>"; } ?> <?php include('fgl_library/footer.php'); ?> </td> </tr> </table> </td> </tr> </table> </div> </body></html> Link to comment Share on other sites More sharing options...
justsomeguy Posted October 25, 2007 Share Posted October 25, 2007 Since there is already a function running onload, it's best to add the code to the existing function. So add this code: hyperlinks = document.getElementsByTagName('a'); for (i = 0; i < hyperlinks.length; i++) { hyperlink = hyperlinks[i].href; extension = hyperlink.split(".")[hyperlink.split(".").length - 1] if (extension != "pdf") { document.getElementsByTagName('a')[i].target = ""; } } Inside the existing startList function (e.g. right before the "if (document.all....)" line). Link to comment Share on other sites More sharing options...
XAceX Posted October 25, 2007 Author Share Posted October 25, 2007 This is what you meant yes??? <html> <head> <title>FGL Customer Services</title> <meta content=""> <LINK href="css/fglweb.css" rel="stylesheet" type="text/css"> <script LANGUAGE="JavaScript" SRC="javascript/menu.js"></SCRIPT> <script type="text/javascript"> startList = function() { hyperlinks = document.getElementsByTagName('a'); for (i = 0; i < hyperlinks.length; i++) { hyperlink = hyperlinks[i].href; extension = hyperlink.split(".")[hyperlink.split(".").length - 1] if (extension != "pdf") { document.getElementsByTagName('a')[i].target = ""; } } if (document.all&&document.getElementById) { navRoot = document.getElementById("nav"); for (i=0; i<navRoot.childNodes.length; i++) { node = navRoot.childNodes[i]; if (node.nodeName=="LI") { node.onmouseover=function() { this.className+=" over"; } node.onmouseout=function() { this.className=this.className.replace(" over", ""); } } } } } window.onload=startList;/************************************************ Contractible Headers script- Dynamic Drive (www.dynamicdrive.com)* This notice must stay intact for legal use. Last updated Mar 23rd, 2004.* Visit [url="http://www.dynamicdrive.com/"]http://www.dynamicdrive.com/[/url] for full source code***********************************************/var enablepersist="on" //Enable saving state of content structure using session cookies? (on/off)var collapseprevious="no" //Collapse previously open content when opening present? (yes/no)if (document.getElementById){document.write('<style type="text/css">')document.write('.switchcontent{display:none;}')document.write('</style>')}function getElementbyClass(classname){ccollect=new Array()var inc=0var alltags=document.all? document.all : document.getElementsByTagName("*")for (i=0; i<alltags.length; i++){if (alltags[i].className==classname)ccollect[inc++]=alltags[i]}}function contractcontent(omit){var inc=0while (ccollect[inc]){if (ccollect[inc].id!=omit)ccollect[inc].style.display="none"inc++}}function expandcontent(cid){if (typeof ccollect!="undefined"){if (collapseprevious=="yes")contractcontent(cid)document.getElementById(cid).style.display=(document.getElementById(cid).style.display!="block")? "block" : "none"}}function revivecontent(){contractcontent("omitnothing")selectedItem=getselectedItem()selectedComponents=selectedItem.split("|")for (i=0; i<selectedComponents.length-1; i++)document.getElementById(selectedComponents[i]).style.display="block"}function get_cookie(Name) { var search = Name + "="var returnvalue = "";if (document.cookie.length > 0) {offset = document.cookie.indexOf(search)if (offset != -1) { offset += search.lengthend = document.cookie.indexOf(";", offset);if (end == -1) end = document.cookie.length;returnvalue=unescape(document.cookie.substring(offset, end))}}return returnvalue;}function getselectedItem(){if (get_cookie(window.location.pathname) != ""){selectedItem=get_cookie(window.location.pathname)return selectedItem}elsereturn ""}function saveswitchstate(){var inc=0, selectedItem=""while (ccollect[inc]){if (ccollect[inc].style.display=="block")selectedItem+=ccollect[inc].id+"|"inc++}document.cookie=window.location.pathname+"="+selectedItem}function do_onload(){uniqueidn=window.location.pathname+"firsttimeload"getElementbyClass("switchcontent")if (enablepersist=="on" && typeof ccollect!="undefined"){document.cookie=(get_cookie(uniqueidn)=="")? uniqueidn+"=1" : uniqueidn+"=0" firsttimeload=(get_cookie(uniqueidn)==1)? 1 : 0 //check if this is 1st page loadif (!firsttimeload)revivecontent()}}(window.addEventListener)window.addEventListener("load", do_onload, false)else if (window.attachEvent)window.attachEvent("onload", do_onload)else if (document.getElementById)window.onload=do_onloadif (enablepersist=="on" && document.getElementById)window.onunload=saveswitchstate</script> </head> It's not working they still open in a new window. Link to comment Share on other sites More sharing options...
justsomeguy Posted October 25, 2007 Share Posted October 25, 2007 It might have a problem with this line:extension = hyperlink.split(".")[hyperlink.split(".").length - 1]Replace that with this:extension = hyperlink.split(".").pop();If that still doesn't work, replace it with this: chunks = hyperlink.split(".");extension = chunks.pop(); Oh wait. And change this: if (extension != "pdf") { document.getElementsByTagName('a')[i].target = "";} To this: if (extension == "pdf") { document.getElementsByTagName('a')[i].target = "_blank";} Link to comment Share on other sites More sharing options...
XAceX Posted October 26, 2007 Author Share Posted October 26, 2007 Dammit all! I tried all that and it still wont do it. Link to comment Share on other sites More sharing options...
justsomeguy Posted October 26, 2007 Share Posted October 26, 2007 Replace the startList function with this and tell me what you see: startList = function() { hyperlinks = document.getElementsByTagName('a'); alert(hyperlinks.length + " links found"); for (i = 0; i < hyperlinks.length; i++) { hyperlink = hyperlinks[i].href; alert("link goes to " + hyperlink); extension = hyperlink.split(".").pop(); if (extension == "pdf") { document.getElementsByTagName('a')[i].target = "_blank"; alert("target set"); } } if (document.all&&document.getElementById) { navRoot = document.getElementById("nav"); for (i=0; i<navRoot.childNodes.length; i++) { node = navRoot.childNodes[i]; if (node.nodeName=="LI") { node.onmouseover=function() { this.className+=" over"; } node.onmouseout=function() { this.className=this.className.replace(" over", ""); } } } } } window.onload=startList; Link to comment Share on other sites More sharing options...
XAceX Posted October 31, 2007 Author Share Posted October 31, 2007 hmmm I see nothing different. Still doing the same thing Link to comment Share on other sites More sharing options...
justsomeguy Posted October 31, 2007 Share Posted October 31, 2007 You should be getting popup boxes showing what the values of various things are:alert(hyperlinks.length + " links found");alert("link goes to " + hyperlink);alert("target set");You're not seeing those popups? If you aren't seeing at least the first one that shows how many links were found that means that either the function isn't getting executed or the first line of the function is causing an error. I'm not sure why that would happen though, getElementsByTagName has been supported by IE since 1998. Link to comment Share on other sites More sharing options...
XAceX Posted October 31, 2007 Author Share Posted October 31, 2007 hmm didn't get any pop ups. But guess what the cool guy who gave me all that code finally decided to just tell me how to do it instead of having me figure it out. He always does that uhggggg. Its like damn just tell me so I don't have to spend days on something simple. sheesh. It was on another page of code all I had to do was erase target=blank. I'm a bit miffed about that. Thanks for your help though. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.