SitePoint Sponsor |
|
User Tag List
Results 1 to 7 of 7
Thread: make link open in new tab/window
-
Oct 15, 2007, 10:36 #1
- Join Date
- Oct 2007
- Location
- Bilthoven, Netherlands
- Posts
- 120
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
make link open in new tab/window
According to Kevin Yanks, it should be possible to make external links open in a new tab/window by using this method (and keep it xhtml strict validated):
http://www.sitepoint.com/article/sta...pliant-world/3
Code:function externalLinks() { if (!document.getElementsByTagName) return; var anchors = document.getElementsByTagName("a"); for (var i=0; i<anchors.length; i++) { var anchor = anchors[i]; if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") anchor.target = "_blank"; } } window.onload = externalLinks;
Code:<script type="text/javascript" src="/external.js"> </script>
Any ideas?
Am I doing something wrong?
PS: I abbreviated rel="external" to rel="ext". When it didn't work, I changed the " "s to ' 's in the javascript.
-
Oct 15, 2007, 11:19 #2
- Join Date
- Nov 2004
- Location
- Nelson BC
- Posts
- 2,310
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Your onload function is getting overwritten by google maps.
-
Oct 15, 2007, 11:21 #3
- Join Date
- May 2006
- Location
- Central Florida
- Posts
- 2,345
- Mentioned
- 192 Post(s)
- Tagged
- 5 Thread(s)
Where is the window.onload statement?
I suspect it is not being called.Don't be yourself. Be someone a little nicer. -Mignon McLaughlin, journalist and author (1913-1983)
►Git is for EVERYONE
►Literally, the best app for readers.
►Make Your P@ssw0rd Secure
►Leveraging SubDomains
-
Oct 15, 2007, 11:47 #4
- Join Date
- Oct 2007
- Location
- Bilthoven, Netherlands
- Posts
- 120
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
thanks - but ..
ParkinT: The "window.onload = externalLinks;" is there, isnt it?
jimfraser:
You are right - on the other pages without the map, it works!
I don't understand why google maps overwrites it? I load the script and I execute it long before the maps-scripts.
I also tried to execute the function the same way the BrowserCheck() function is executed below - no effect.
Any suggestions?
Code:<script type="text/javascript" src="inc/BrowserCheck.js"> </script> <script type="text/javascript" src="inc/ExternalLinks.js"> </script> </head> <body> <div id='browalert'></div> <div id='wrapper'> <script type="text/javascript"> BrowserCheck(); </script>
-
Oct 15, 2007, 11:57 #5
- Join Date
- Nov 2004
- Location
- Nelson BC
- Posts
- 2,310
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Well, you're defining window.onload and then later on google maps redefines it, so as with any coding the last value to which you set a variable is the one that sticks around.
Best way would be to redefine it again, that way you won't have to mess around with google maps js files (which you might upgrade/change/etc).
Due to the fact that your JS files are scattered throughout the html, put this near the end of your html (before </body> closing tag):
Code:<script type="text/javascript"> window.onload = function () { externalLinks(); // your function init(); // google maps init function } </script>
-
Oct 15, 2007, 12:37 #6
- Join Date
- Oct 2007
- Location
- Bilthoven, Netherlands
- Posts
- 120
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thanks - makes good sense. Will try that tomorrow. It's 21:37 here ...
-
Oct 16, 2007, 01:02 #7
- Join Date
- Oct 2007
- Location
- Bilthoven, Netherlands
- Posts
- 120
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thanks jimfraser, that did the trick!
Although, it doesn't work for the links in the inc/map_data.php file - links that occur in the white info-bubbles when you click the markers in the map. I would like them to open in a new tab using the rel='ext', but they won't. Not even when I make the externalLinks() function run AFTER the init() and after importing inc/map_data.php.
Code:<script type="text/javascript"> window.onload = function () { init(); // from map_functions.js externalLinks(); // from ExternalLinks.js } </script>
Bookmarks