karimali831 Posted February 21, 2011 Share Posted February 21, 2011 Hi I want all hyperlink targets to be "_top" in a specific directory. Not sure if I can do this with JS or anything.. but thought I'd ask Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/228388-same-hyperlink-targets/ Share on other sites More sharing options...
.josh Posted February 21, 2011 Share Posted February 21, 2011 what do you mean? Are you saying you want all "/xyz/somePage.html" links to have target="_top" attribute? Sure, you can do this with javascript. Easiest way is with a library like jQuery: <script type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js'></script> <script type='text/javascript'> $(document).ready(function() { // change /xyz/ to your directory $("a[href*='/xyz/']").attr('target','_top'); }); </script> You can do something similar in Prototype or some other js library if you are already using one on your site (syntax will be slightly different). Or if you don't want to use a framework, you can do it with pure js with getElementsByTagName(), a for loop, getAttribute(), an if condition, and setAttribute(). Or you could of course just hardcode target="_top" in all your relative link tags... Quote Link to comment https://forums.phpfreaks.com/topic/228388-same-hyperlink-targets/#findComment-1177720 Share on other sites More sharing options...
karimali831 Posted February 25, 2011 Author Share Posted February 25, 2011 Thanks for this but I need one condition to it: If the hyperlink has this included: "?site=" then every link must have "_top" as target. Can I use this condition ? Quote Link to comment https://forums.phpfreaks.com/topic/228388-same-hyperlink-targets/#findComment-1179492 Share on other sites More sharing options...
trq Posted February 25, 2011 Share Posted February 25, 2011 Indeed you can, see http://api.jquery.com/category/selectors/attribute-selectors/ Quote Link to comment https://forums.phpfreaks.com/topic/228388-same-hyperlink-targets/#findComment-1179495 Share on other sites More sharing options...
karimali831 Posted February 25, 2011 Author Share Posted February 25, 2011 Can you or someone please amend Crayon Violent's code to apply this condition? I will learn this way, thank you! Quote Link to comment https://forums.phpfreaks.com/topic/228388-same-hyperlink-targets/#findComment-1179531 Share on other sites More sharing options...
.josh Posted February 25, 2011 Share Posted February 25, 2011 Can you or someone please amend Crayon Violent's code to apply this condition? I will learn this way, thank you! No you won't. The only thing you will learn by having people just hand stuff to you is laziness. Use your brain. I provided you with code that will look for /xyz/ in the url. How hard do you think it really is to replace "/xyz/" with "?site=" ? I assure you, it is not hard at all, and if you weren't so busy trying to freeload, you would see it easy enough. Quote Link to comment https://forums.phpfreaks.com/topic/228388-same-hyperlink-targets/#findComment-1179537 Share on other sites More sharing options...
karimali831 Posted February 25, 2011 Author Share Posted February 25, 2011 soo how do you do it? Quote Link to comment https://forums.phpfreaks.com/topic/228388-same-hyperlink-targets/#findComment-1179546 Share on other sites More sharing options...
trq Posted February 26, 2011 Share Posted February 26, 2011 soo how do you do it? Read the link I provided or quit trying to program. Quote Link to comment https://forums.phpfreaks.com/topic/228388-same-hyperlink-targets/#findComment-1179832 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.