Open Links in New Window if Different Domain Name
Hi,
How could I very simply make a page open in a new window if the domain name in the href attribute is different from a certain value ?
I would like this to be applied to all links in current page.
Big thanks in advance.
kb posted this at 16:38 — 18th September 2004.
He has: 1,380 posts
Joined: Feb 2002
I'm not sure I understand your question, but what you might be looking for is:
<a href="mysite.com" target="_new">MySite</a>
Which is a perfectly fine way of opening a new window. As far as comparing goes, you're going to need a language. The following is untested, so no guaruntees.
<?php
// Url refers to the link which would have to be grabbed somehow
// Main refers to the actual site you're running
If ($main != (substr($url, 0, strpos($url, \".com\"))) {
echo \"<a href=\\"$url\\" target=\\"_new\\">the site</a>\";
} else {
echo \"<a href=\\"$url\\">the site</a>\";
};
?>
eSite posted this at 21:47 — 18th September 2004.
They have: 11 posts
Joined: Sep 2004
The target attribute is no more valid XHTML Strict 1.0
A PHP echo for each link is a horrible solution.
I thought of a plug&play JavaScript that would check all links in the page.
fifeclub posted this at 18:15 — 20th September 2004.
He has: 688 posts
Joined: Feb 2001
I use target attributes all the time and (somebody correct me if I'm wrong) they are XHTML strict compliant. What is illegal is using target="_new", but target="new" works fine and validates. I can't remember the reason why but for some reason they 'outlawed' the use of the underscore, but the target attribute itself should be fine. Correct?
kb posted this at 21:52 — 18th September 2004.
He has: 1,380 posts
Joined: Feb 2002
Well, first off, you didnt' give me/us enough information to really do anything
Secondly, it is not a horrible solution...I was showing you how you can CHECK for it being in the same site or not, and then expected you to work with it. So, technically, it's not even a solution.
Third, Javascipt is, to use your terminology, a "horrible solution" because many people hava Javascript turned off, so it would do nothing. It needs to be a server-side activity.
eSite posted this at 22:11 — 18th September 2004.
They have: 11 posts
Joined: Sep 2004
Well if we don't choose new technologies because minorities aren't using them, humanity progress will stop right away.
So javascript is a good solution and even the most used for the purpose
I described.
I base my request on this article.
(The problem with the script described there is that it's not plug&play)
Greg K posted this at 00:06 — 19th September 2004.
He has: 2,145 posts
Joined: Nov 2003
I think we can all agree this topic got off to a bad start from lack of communicating needs. (ie. needing javascript, needing it to be XHTML Strict 1.0 compliant, that you had a script that gave you the idea, etc.)
I was wondering though where you got the statisics that using javascript is the most used for opening a link in a new window?
Also, keep in mind, Javascript is hardly a "new technology".
Now to help you in what you are looking for, what are you meaning by having the script being plug & play?
BTW, thank you for the link, I enjoyed it.
-Greg
eSite posted this at 00:16 — 19th September 2004.
They have: 11 posts
Joined: Sep 2004
I don't have statistics, but what other language could do the job and keep the page valid ?
Plug&play means that we have a valid XHTML 1.0 Strict page with hyperlinks.
At this step all links open in the same window.
Then we add the javascript (in head) and links, that do not point to the domain where the page is hosted, do open in a new window.
kb posted this at 02:25 — 19th September 2004.
He has: 1,380 posts
Joined: Feb 2002
May I ask what the purpose of this is? Why can't you just code certain links to be opened in a new window and others not?
Greg K posted this at 08:21 — 19th September 2004.
He has: 2,145 posts
Joined: Nov 2003
I can see the need for it. I maintain a college server, and since I don't have time to edit the content, the practice of opening a new window for all links not on our server gets forgotten (the person who updates the content is not a web person).
So in my case, it would be easier to add a call to javascript on the one template file instead of checking for links on 300+ pages. (This is why I was glad to see the link to the article, as I think I will try to impliment this on the site).
-Greg
eSite posted this at 10:34 — 19th September 2004.
They have: 11 posts
Joined: Sep 2004
Why would I waste time adding code for each links ?
When I'll update to CSS3 all this would be useless.
My site is a blog, there are about 10 links to my domain, all the other are externals.
I posted here because I don't javascript at all.
Are you guys capable of creating such a script ?
Some suggestions.
kb posted this at 14:44 — 19th September 2004.
He has: 1,380 posts
Joined: Feb 2002
Why don't you try coding it yourself? Instead of depending on us to do everything for you, from conceptualizing to completion, why don't you give it a bang, and then when it doesn't work, come back and we'll help you.
eSite posted this at 14:55 — 19th September 2004.
They have: 11 posts
Joined: Sep 2004
I don't try coding it myself because as I said above I don't know javascript at all.
And no I'm not gonna learn JS first for 2 months and then try.
I know very precisely what I need, but I ignore what the corresponding code is.
Now or you know what that code is, either you stop wasting our time.
kb posted this at 16:10 — 19th September 2004.
He has: 1,380 posts
Joined: Feb 2002
(Does anyone else sense the hostility?)
I don't appreciate the manner in which you are treating this, as we are a community forum for help, not for free-contract work. We try to help you get ideas, and know where to start, but 99times out of 100, we dont write everything for you. I gave you what I thought was a good start on a path that I thought you meant, and you reacted with hostility.
Dragon of Ice posted this at 16:30 — 19th September 2004.
He has: 578 posts
Joined: Jun 2004
Alright people, let's keep the flames down.
modulargaming posted this at 17:32 — 19th September 2004.
He has: 152 posts
Joined: Jun 2004
does anybody know where i can learn javascript
Abhishek Reddy posted this at 12:40 — 20th September 2004.
He has: 3,348 posts
Joined: Jul 2001
*sigh*
Humanity? Progress? Javascript?!
target attribute is being phased out for a reason.
When will people learn? Don't mess with the client. If I want that link to open in a new window (or tab), I'll middle-click it myself. Touch not my window object!
Anyhow, Kyle is quite correct in saying that we're not here to manufacture scripts for the highest bidder (rather, any bidder). If you're asking for help in writing your own, we'll be glad to pitch in. Post up what code you have here and try to describe what limitations it has, or what you want fixed. We can try advancing from there.
Sauron's Vault, javascriptkit.com
fifeclub posted this at 20:10 — 20th September 2004.
He has: 688 posts
Joined: Feb 2001
I stand corrected. It is allowed in XHTML Transitional. So the question is, why use Strict?
http://www.zeldman.com/daily/0503a.shtml#strictlyspeaking
kb posted this at 21:52 — 20th September 2004.
He has: 1,380 posts
Joined: Feb 2002
So, Abhi, how are we supposed to open new windows without using Javascript (wouldn't anyways), and without using target="new"?
Abhishek Reddy posted this at 03:53 — 21st September 2004.
He has: 3,348 posts
Joined: Jul 2001
Ah, that's my point. Don't.
KeithMcL posted this at 14:15 — 21st September 2004.
He has: 176 posts
Joined: Oct 1999
I think this article might help you out:
http://www.sitepoint.com/article/xhtml-strict-popups
eSite posted this at 14:28 — 21st September 2004.
They have: 11 posts
Joined: Sep 2004
Sorry guys but I'm just extremely hard to please.
I though I were not the first to ask for a script that already exists somewhere you might know where, or at least that there were some gurus here that would have wrote the script in a minute.
kb posted this at 19:37 — 21st September 2004.
He has: 1,380 posts
Joined: Feb 2002
I'm sorry...but you seem to be nothing but condescending in every one of your posts, and cant get by without some jab at our skills (individually or as a group).
Maybe you should consider changing your posting attitude
Abhishek Reddy posted this at 21:44 — 21st September 2004.
He has: 3,348 posts
Joined: Jul 2001
Ease up, I didn't see a jab in that last post.
eSite, it seemed like you were requesting this of us as a service, rather than requesting for help -- we are more likely to happily do the latter than the former. If you want someone to write up a script for you, post an offer in the Market Place section of this site.
If you don't want to write up code, why should we? It is only if you have the will to solve your problem, but lack some knowledge, expertise, or another pair of eyes that we'll try to do something about it.
You didn't really make it clear that you wanted links or pre-made scripts either; it appeared to me like you wanted one of us to do the dirty work for you the whole time.
Do you see why we're unwilling to do this for you?
Perhaps this is just a communication issue. Why don't we leave it at that and proceed without the hostility.
Again, please, if you have some code already that you have trouble with, feel free to let us see it and work on it.
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.