jQuery - Return true only if anchor href contains URL only












0















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.










share|improve this question



























    0















    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.










    share|improve this question

























      0












      0








      0








      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.










      share|improve this question














      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 21 '18 at 5:25









      MangeshMangesh

      44




      44
























          1 Answer
          1






          active

          oldest

          votes


















          1














          Use the selector string a[href^="http"] to select as 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 hrefs and not just external links or absolute paths, you could add a[href^="/"] as well, to match as with relative URLs:



          $('a[href^="http"], a[href^="/"]').each(function() {





          share|improve this answer


























          • 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











          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
          });


          }
          });














          draft saved

          draft discarded


















          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









          1














          Use the selector string a[href^="http"] to select as 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 hrefs and not just external links or absolute paths, you could add a[href^="/"] as well, to match as with relative URLs:



          $('a[href^="http"], a[href^="/"]').each(function() {





          share|improve this answer


























          • 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
















          1














          Use the selector string a[href^="http"] to select as 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 hrefs and not just external links or absolute paths, you could add a[href^="/"] as well, to match as with relative URLs:



          $('a[href^="http"], a[href^="/"]').each(function() {





          share|improve this answer


























          • 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














          1












          1








          1







          Use the selector string a[href^="http"] to select as 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 hrefs and not just external links or absolute paths, you could add a[href^="/"] as well, to match as with relative URLs:



          $('a[href^="http"], a[href^="/"]').each(function() {





          share|improve this answer















          Use the selector string a[href^="http"] to select as 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 hrefs and not just external links or absolute paths, you could add a[href^="/"] as well, to match as with relative URLs:



          $('a[href^="http"], a[href^="/"]').each(function() {






          share|improve this answer














          share|improve this answer



          share|improve this answer








          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



















          • 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




















          draft saved

          draft discarded




















































          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.




          draft saved


          draft discarded














          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





















































          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







          這個網誌中的熱門文章

          Tangent Lines Diagram Along Smooth Curve

          Yusuf al-Mu'taman ibn Hud

          Zucchini