Hi all,

I found a script on dynamic drive that makes all links linking to a page that is on another website open in a _blank window. (http://www.dynamicdrive.com/dynamicindex8/newwindow2.htm)

I have just finished creating a page which has 2 frames:

1) At the top with my logo and menu on it
2) At the bottom which gets it's content using PHP and grabbing the url (index.php?url=blah.com) from the address bar.

My question:

How would I edit the script i found on dynamic drive (http://www.dynamicdrive.com/dynamicindex8/newwindow2.htm) in order to find links that link to an external website, and edit the links href to http://www.mysite.com/externallinks/?url=*origional url*

Thanks all :)

p.s. im not great with javascript :P

Recommended Answers

All 3 Replies

Member Avatar for langsor

Easy-breezy...

Instead of having target="_blank" have target="name_of_frame"

I would be using <iframes> instead of old-skool frames, but the same principle applies either way.

<a href="http://google.com" target="framed">Open link</a><br />
<iframe name="framed"></iframe>

You can style iframes like any other html element using CSS -- which is vernice

To excerpt their code

change ...

//2) Target for links that should open in a new window (ie: "_blank", "secwin" etc):
linktarget: "_blank",

... to ...

//2) Target for links that should open in a new window (ie: "_blank", "secwin" etc):
linktarget: "framed", // or the name of your frame-window

Hope this helps ....

Ahh okay that makes sense. The only problem is that the link will expect that the target frame is on the same page, however I need to edit the url of the link and change it like this:

origional:
http://www.google.com

changed to:
http://www.economizerz.com/externallinks/?url=http://www.google.com

So if i change the url I dont need to change the frame because in my externallinks index page the bottom frame takes it's source using PHP's get function out of the address bar so the bottom frame will load the value of url (http://www.google.com)

What I have done so far is added a couple of simple lines in.

Old Code:

assigntarget:function(){
	var rexcludedomains=new RegExp(this.excludedomains.join("|"), "i")
	var all_links=document.getElementsByTagName("a")
	if (this.mode=="auto" || (this.mode=="manual" && this.togglebox.checked)){
		for (var i=0; i<=(all_links.length-1); i++){
			if (all_links[i].hostname.search(rexcludedomains)==-1 && all_links[i].href.indexOf("http:")!=-1)
				all_links[i].target=ddwindowlinks.linktarget

New Code:

assigntarget:function(){
	var rexcludedomains=new RegExp(this.excludedomains.join("|"), "i")
	var all_links=document.getElementsByTagName("a")
	if (this.mode=="auto" || (this.mode=="manual" && this.togglebox.checked)){
		for (var i=0; i<=(all_links.length-1); i++){
			if (all_links[i].hostname.search(rexcludedomains)==-1 && all_links[i].href.indexOf("http:")!=-1)
				all_links[i].target=ddwindowlinks.linktarget
				var origionalhref = all_links[i].href;
				all_links[i].href='http://www.economizerz.com/externallinks/?url='.origionalhref

So by that I have told it to change the href on the link to
http://www.economizerz.com/externallinks/?url=
but the problem im having is adding the origional href onto the end. I thought that by setting a variable (origionalhref) to the value of the origional link's href it might work but it comes up with undefined so im guessing this syntax is wrong:
var origionalhref = all_links.href;

Any ideas how to fix it? Im basically storing the origional value of the link's href in a variable before changing it, and calling it later in the script when i want to change that link's href.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.