简体   繁体   中英

Credit link not work according to the given my script

Well i am coding a script which will avoid editing of credits here is my total page code:

//start the script after loading the page
$(document).ready(function(){
//get the elements from page
var urlvalue1 = document.getElementById("#mycredit").href;


//correct link
if (urlvalue1 == "http://themedaddy.net" )
{window.location.replace("#");
}
//edited link
else{
window.location.replace("http://themedaddy.net");
}

But it is not working even i have added the following html code in the body section and linked the jquery script to it.

<a href="http://themedaddy444444.net" id="mycredit">ThemeDaddy</a>

According to the script it must need to get redirected as the link value is not satisfying.

wrap up the redirect code inside the click event of the anchor tag.

$('a#mycredit').on('click',function(e){
  e.preventDefault();
  //correct link
  if (urlvalue1 == "http://themedaddy.net" )
    window.location.replace("#");
  else{//edited link
   window.location.replace("http://themedaddy.net");
  }
});

Its working fine you have only mistakenly written #mycredit instead of mycredit while selecting the anchor tag.

$(document).ready(function(){

        var urlvalue1 = document.getElementById("mycredit").href;
        if (urlvalue1 == "http://themedaddy.net" )
        {
            window.location.replace("#");
        }
        //edited link
        else{
        window.location.replace("http://themedaddy.net");
        }

    });
    </script>
    <a href="http://themedaddy444444.net" id="mycredit">ThemeDaddy</a>

See this is working absolutely fine

In selecting elements with getElementById we do not need to put # sign before Id :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM