/    Sign up×
Community /Pin to ProfileBookmark

Does JavaScript have "starts with" function like CSS?

CSS has a “starts with” function for links:
a[href^=”http://www.yahoo.com“] {color: red;}

The above line reads: “If the link starts with the URL http://www.yahoo.com, then make the link colored RED”. The “starts with” part is the little ^ symbol before the link.

That’s fine to use if I want to STYLE a link in a certain way depending on what the link starts with, but if I want the link to BEHAVE in a different way depending on what that link starts with, I cannot use a Cascading STYLE Sheet, I must use JavaScript.

Does anyone know if JavaScript has a similar function that allows me to make a link behave in a certain way depending on what it starts with??? For example, I might want the link to open in a certain frame or new window.

Thank you for your help.

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@VladdyMar 16.2004 — CSS does not have functions - it has selectors.

Javascript allows you to write functions to your liking

Why don't you let the user decide where they want to open a link? Thinking that you know better than them is a bit arrogant, don't you think?
Copy linkTweet thisAlerts:
@jaegernautMar 16.2004 — I have a couple of questions for you:

1. Is that the functionality you want? IE different destinations for the links to open in, or was that just an example?

2. Are the number of links dynamic, or do you know how many there will be?

Of course, if you are just wanting to point certain links to open in a new window or in a specific frame you can just use the target attribute in the href to guide that.
×

Success!

Help @HGJS spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...