mesh2005 Posted April 17, 2008 Share Posted April 17, 2008 Without using target=_blank, is this possible? Thank you Quote Link to comment https://forums.phpfreaks.com/topic/101542-solved-can-i-force-all-hyperlinks-in-a-ltdivgt-to-open-in-a-new-window/ Share on other sites More sharing options...
rhodesa Posted April 17, 2008 Share Posted April 17, 2008 Is it that you CAN'T use target=_blank? Or that you have links, in a DIV, without that attribute. Can the attribute be added dynamically with JavaScript after the page loads? Quote Link to comment https://forums.phpfreaks.com/topic/101542-solved-can-i-force-all-hyperlinks-in-a-ltdivgt-to-open-in-a-new-window/#findComment-519492 Share on other sites More sharing options...
ToonMariner Posted April 17, 2008 Share Posted April 17, 2008 yes I would restrict this to javscript though... give the div a unique id (if its just one div you want to do this on in a page) or a class if you have a couple of divs you want to make this happen... I will use the class example... <div class="external-links"> ... [links] ... </div> now the javascript... window.onload = checkExternalLinks(); function checkExternalLinks() { var divs = document.getElementsByTagName('div'); var links = Array(); var i = 0; for(var x=0; x<divs.length; x++) { if (divs[x].className == 'external-links') { links = divs[x].getElementsByTagName('a'); for(i=0;i<links.length;i++) { links[x].onclick = function() { return newPage(this); }; } } } } function newPage(obj) { window.open(obj.href); return false; } Try that... Quote Link to comment https://forums.phpfreaks.com/topic/101542-solved-can-i-force-all-hyperlinks-in-a-ltdivgt-to-open-in-a-new-window/#findComment-519500 Share on other sites More sharing options...
haku Posted April 17, 2008 Share Posted April 17, 2008 Please don't. There is nothing more annoying than sites where all (or even any) links open in external windows. It takes control away from me the user, which takes away from my experience. Quote Link to comment https://forums.phpfreaks.com/topic/101542-solved-can-i-force-all-hyperlinks-in-a-ltdivgt-to-open-in-a-new-window/#findComment-519612 Share on other sites More sharing options...
ToonMariner Posted April 17, 2008 Share Posted April 17, 2008 well - some pages should open in a new window (and the user should be informed that they will open in a new window!!!) but it is definitely something that should be solely controlled by javascript as it is a document effect... Quote Link to comment https://forums.phpfreaks.com/topic/101542-solved-can-i-force-all-hyperlinks-in-a-ltdivgt-to-open-in-a-new-window/#findComment-519877 Share on other sites More sharing options...
haku Posted April 17, 2008 Share Posted April 17, 2008 Im of the belief that almost NO images should open in an external window, unless that is central to the functionality of the window being opened. If I want to open a link in a new window I will (and do). But that should be my choice as the user, not the developer/designers choice. Quote Link to comment https://forums.phpfreaks.com/topic/101542-solved-can-i-force-all-hyperlinks-in-a-ltdivgt-to-open-in-a-new-window/#findComment-520012 Share on other sites More sharing options...
mesh2005 Posted April 18, 2008 Author Share Posted April 18, 2008 Thank you very much for your replies and special thanks to ToonMariner. I need to force the links for some reason, actually the site owner wanna me to do so The Javascript code was not working correctly, I modified it. Here is the code: function checkExternalLinks() { var divs =document.getElementsByTagName('div'); var links = Array(); var i = 0; for(var x=0; x<divs.length; x++) { if (divs[x].className == 'external-links') { links = divs[x].getElementsByTagName('a'); for(i=0;i<links.length;i++) { links[i].target = '_blank'; } } } } Thanks again Quote Link to comment https://forums.phpfreaks.com/topic/101542-solved-can-i-force-all-hyperlinks-in-a-ltdivgt-to-open-in-a-new-window/#findComment-520802 Share on other sites More sharing options...
ToonMariner Posted April 18, 2008 Share Posted April 18, 2008 well I aint perfect lol The principle was sound... disappointing you have to use '_blank' what errors did teh code I posted return? I have never run the code so I don't know what it would do... Quote Link to comment https://forums.phpfreaks.com/topic/101542-solved-can-i-force-all-hyperlinks-in-a-ltdivgt-to-open-in-a-new-window/#findComment-520834 Share on other sites More sharing options...
zonkd Posted April 19, 2008 Share Posted April 19, 2008 I'm trying to get a blog link in a popup to open in a new page. I've tried every variation I can think of so that that the blog page doesn't open in the confined space of the popup. But so far, no joy. I'll try the script here - but, mesh2005, is that the complete script with your corrections? Quote Link to comment https://forums.phpfreaks.com/topic/101542-solved-can-i-force-all-hyperlinks-in-a-ltdivgt-to-open-in-a-new-window/#findComment-521198 Share on other sites More sharing options...
zonkd Posted April 19, 2008 Share Posted April 19, 2008 No, doesn't move out of the popup. Pity. Quote Link to comment https://forums.phpfreaks.com/topic/101542-solved-can-i-force-all-hyperlinks-in-a-ltdivgt-to-open-in-a-new-window/#findComment-521204 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.