Home > other >  Redirect website if the content is difference
Redirect website if the content is difference

Time:09-18

I created a code to redirect a website in case the content of the link is different, it redirects to my website. But, the website is being redirect even if the content is exactly equal. What's wrong with my javascript code?

Ps.: Yes, I know the client may delete the code, and etc. I just want to know what's wrong with code.

<div id="credits">
   <span class='credits'>Design <a href='https://portfolio.com' target='_blank'>Designer</a></span>
</div>

<script type='text/javascript'>
   $(document).ready(function() {
      if ($('span.credits > a') != 'https://portfolio.com') {
         window.location = 'https://portfolio.com'
      } else {
         return False;
      }
   });
</script>

CodePudding user response:

You should be comparing the element's href attribute, not the element itself:

$(document).ready(function() {
  if ($('span.credits > a').attr('href') != 'https://portfolio.com') {
    window.location = 'https://portfolio.com'
  } else {
    return false;
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="credits">
  <span class='credits'>Design <a href='https://portfolio.com' target='_blank'>Designer</a></span>
</div>

  • Related