简体   繁体   中英

Javascript - Where i'm making a mistake?

i'm creating that script if someone delete the text or link on footer of my template it will redirect to my site, but its redirecting without removing anything.

function loadBody() {
 var elem = document.getElementById("mycontent");
if(elem.innerHTML !== "MySite" || elem.href !== "http://mysite.com")
  window.location="mysite.com"; 
}

here is the footer html

 <div id='mycontent'>       
  <p>&#169; Template is designed by  <a href='http://mysite.com' title=''>Mysite</a></p>
  </div>

thanks in advance.

This doesn't work because of the structure:

function loadBody() {
    var elem = document.getElementById("mycontent");
if(elem.innerHTML !== "MySite" || elem.href !== "http://mysite.com")
    window.location="mysite.com"; 
}

<div id='mycontent'>       
    <p>&#169; Template is designed by  <a href='http://mysite.com' title=''>Mysite</a></p>
</div>

You are assuming thag the a tag has the mycontent id attribute, but it is on the div above it. So, either you change for this:

<div>       
    <p>&#169; Template is designed by  <a id='mycontent' href='http://mysite.com' title=''>Mysite</a></p>
</div>

Or you have to alter the verification on Javascript.

Did you forget http:// ? + EDIT: use getAttribute. Updated with the new innerHTML

function loadBody() {
 var elem = document.getElementById("mycontent");
if(elem.innerHTML != "<p>&#169; Template is designed by  <a href='http://mysite.com' title=''>Mysite</a></p>")
  window.location="mysite.com"; 
}

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