jQuery - Return true only if anchor href contains URL only
I have one jQuery function which executes for each anchor tag from the page which needs to be checked that acnhor tag should contains herf attribute with URL of any site. It should return false in case if anchor tag does not contains href tag or href tag having the values link blank, mailto, #, tel:1000 etc.
Means condition should check anchor tag should have href tag & hreg tag should contain URL only.
$('a').each(function() { //add a class to all external links
var $a = jQuery(this);
var domainURL = $a.get(0).href;
var hostToDetectAsExternal = domainURL.replace('http://','').replace('https://','').replace('www.','').split(/[/?#]/)[0];
if(domainURL != '' && domainURL.indexOf('javascript') == -1 && allwedHostsArray.indexOf(hostToDetectAsExternal) == -1) // -1 : Not found in a allowed hosts array & not found javacript in a URL
{
$a.addClass('external-link');
}
});
have written this code but not fulfilling all the conditions. Please anyone help me out.
jquery html
add a comment |
I have one jQuery function which executes for each anchor tag from the page which needs to be checked that acnhor tag should contains herf attribute with URL of any site. It should return false in case if anchor tag does not contains href tag or href tag having the values link blank, mailto, #, tel:1000 etc.
Means condition should check anchor tag should have href tag & hreg tag should contain URL only.
$('a').each(function() { //add a class to all external links
var $a = jQuery(this);
var domainURL = $a.get(0).href;
var hostToDetectAsExternal = domainURL.replace('http://','').replace('https://','').replace('www.','').split(/[/?#]/)[0];
if(domainURL != '' && domainURL.indexOf('javascript') == -1 && allwedHostsArray.indexOf(hostToDetectAsExternal) == -1) // -1 : Not found in a allowed hosts array & not found javacript in a URL
{
$a.addClass('external-link');
}
});
have written this code but not fulfilling all the conditions. Please anyone help me out.
jquery html
add a comment |
I have one jQuery function which executes for each anchor tag from the page which needs to be checked that acnhor tag should contains herf attribute with URL of any site. It should return false in case if anchor tag does not contains href tag or href tag having the values link blank, mailto, #, tel:1000 etc.
Means condition should check anchor tag should have href tag & hreg tag should contain URL only.
$('a').each(function() { //add a class to all external links
var $a = jQuery(this);
var domainURL = $a.get(0).href;
var hostToDetectAsExternal = domainURL.replace('http://','').replace('https://','').replace('www.','').split(/[/?#]/)[0];
if(domainURL != '' && domainURL.indexOf('javascript') == -1 && allwedHostsArray.indexOf(hostToDetectAsExternal) == -1) // -1 : Not found in a allowed hosts array & not found javacript in a URL
{
$a.addClass('external-link');
}
});
have written this code but not fulfilling all the conditions. Please anyone help me out.
jquery html
I have one jQuery function which executes for each anchor tag from the page which needs to be checked that acnhor tag should contains herf attribute with URL of any site. It should return false in case if anchor tag does not contains href tag or href tag having the values link blank, mailto, #, tel:1000 etc.
Means condition should check anchor tag should have href tag & hreg tag should contain URL only.
$('a').each(function() { //add a class to all external links
var $a = jQuery(this);
var domainURL = $a.get(0).href;
var hostToDetectAsExternal = domainURL.replace('http://','').replace('https://','').replace('www.','').split(/[/?#]/)[0];
if(domainURL != '' && domainURL.indexOf('javascript') == -1 && allwedHostsArray.indexOf(hostToDetectAsExternal) == -1) // -1 : Not found in a allowed hosts array & not found javacript in a URL
{
$a.addClass('external-link');
}
});
have written this code but not fulfilling all the conditions. Please anyone help me out.
jquery html
jquery html
asked Nov 21 '18 at 5:25
MangeshMangesh
44
44
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Use the selector string a[href^="http"]
to select a
s whose href
attributes start with http
(which, of course, will also include those which start with https
)
$('a[href^="http"]').each(function() {
If you wanted to include all URL-based href
s and not just external links or absolute paths, you could add a[href^="/"]
as well, to match a
s with relative URLs:
$('a[href^="http"], a[href^="/"]').each(function() {
When an answer solves your problem, consider marking it as Accepted to indicate that the issue is resolved :)
– CertainPerformance
Nov 21 '18 at 6:01
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53405723%2fjquery-return-true-only-if-anchor-href-contains-url-only%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Use the selector string a[href^="http"]
to select a
s whose href
attributes start with http
(which, of course, will also include those which start with https
)
$('a[href^="http"]').each(function() {
If you wanted to include all URL-based href
s and not just external links or absolute paths, you could add a[href^="/"]
as well, to match a
s with relative URLs:
$('a[href^="http"], a[href^="/"]').each(function() {
When an answer solves your problem, consider marking it as Accepted to indicate that the issue is resolved :)
– CertainPerformance
Nov 21 '18 at 6:01
add a comment |
Use the selector string a[href^="http"]
to select a
s whose href
attributes start with http
(which, of course, will also include those which start with https
)
$('a[href^="http"]').each(function() {
If you wanted to include all URL-based href
s and not just external links or absolute paths, you could add a[href^="/"]
as well, to match a
s with relative URLs:
$('a[href^="http"], a[href^="/"]').each(function() {
When an answer solves your problem, consider marking it as Accepted to indicate that the issue is resolved :)
– CertainPerformance
Nov 21 '18 at 6:01
add a comment |
Use the selector string a[href^="http"]
to select a
s whose href
attributes start with http
(which, of course, will also include those which start with https
)
$('a[href^="http"]').each(function() {
If you wanted to include all URL-based href
s and not just external links or absolute paths, you could add a[href^="/"]
as well, to match a
s with relative URLs:
$('a[href^="http"], a[href^="/"]').each(function() {
Use the selector string a[href^="http"]
to select a
s whose href
attributes start with http
(which, of course, will also include those which start with https
)
$('a[href^="http"]').each(function() {
If you wanted to include all URL-based href
s and not just external links or absolute paths, you could add a[href^="/"]
as well, to match a
s with relative URLs:
$('a[href^="http"], a[href^="/"]').each(function() {
edited Nov 21 '18 at 5:33
answered Nov 21 '18 at 5:27
CertainPerformanceCertainPerformance
90.7k165179
90.7k165179
When an answer solves your problem, consider marking it as Accepted to indicate that the issue is resolved :)
– CertainPerformance
Nov 21 '18 at 6:01
add a comment |
When an answer solves your problem, consider marking it as Accepted to indicate that the issue is resolved :)
– CertainPerformance
Nov 21 '18 at 6:01
When an answer solves your problem, consider marking it as Accepted to indicate that the issue is resolved :)
– CertainPerformance
Nov 21 '18 at 6:01
When an answer solves your problem, consider marking it as Accepted to indicate that the issue is resolved :)
– CertainPerformance
Nov 21 '18 at 6:01
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53405723%2fjquery-return-true-only-if-anchor-href-contains-url-only%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown