简体   繁体   中英

Checking for empty “href” and appending anchor text if it is empty

I have a function in Javascript , that is supposed to iterate through a table of links to check if an href is empty. If the href is empty, it adds a block of text between the anchors to indicate no link added yet. The code I have is below:

function isWorking(){
    //Variable declaration
    var anchor, rows, i, link, x, y;
    anchor = document.getElementById("myTable");
    rows = anchor.getElementsByTagName("TR");
    for(i = 0; i < (rows.length - 1); i++){
        x = rows[i].getElementsByTagName("A");
        link = x.getAttribute("href");
        y = link.getElementsByTagName("FONT")[0];
        if(link = ""){
            y.innerHTML += "<b><font color=red> (not added yet)</font></b>";
        }
    }
}

Currently, it doesn't really do anything. I'm not very proficient enough in Javascript so I am having a really hard time understanding what may need to change to make this code work correctly.

Edit: So here is an example of the table I'm using in HTML:

    <table id="myTable">
            <tr><td><a href="" target="_blank"><font size="4" color="white">C</a></font></td></tr>
        <tr><td><a href="" target="_blank"><font size="4" color="white">A</a></font></td></tr>
        <tr><td><a href="" target="_blank"><font size="4" color="white">B</a></font></td></tr>
    </table>

In the example I include 3 blank links (A, B, and C). Since each links href is empty, I want to append the text with a the text in my if in the code.

if(link = "") is invalid.. You are defining the value by the = operator. It should be if(link == "") .. Your code can also be minified - don't really know what you're defining the table and rows for. Just to get the a link? A better way:

var links = document.querySelectorAll('#mydiv a'),
// gets all a links inside [mydiv] div...
i, href;

for(i = 0; i < links.length; i++) {
  href = links[i].getAttribute('href');
  //check the length of href.. is it less than 1 or is a space
  if(href.trim().length < 1) {
    alert(i + ' is null: ' + href);
  }

}

Here is a working example

Below line has error

link = x.getAttribute("href");

x = rows[i].getElementsByTagName("A"); This statement returns a collection of elements. So to use the function getAttribute, you should apply it on element like below

if(x.length>0)
link = x[0].getAttribute("href");

Your next statements needs modifications accordingly. link.GetElementByTagName will not work as the link does not have any DOM element but just a string value.

You can try it live here

quick and simple:

function checkTableHref() {
        var elem = document.getElementsByTagName('a');
        for (i = 0; i < elem.length; i++) {
            if (elem[i].getAttribute("href") == "") {
                alert(null);
            }
            else {
                alert("Not Null");
            }
        }
    };

and html:

<table id="myTable">
    <tr>
        <td><a href="" target="_blank"><font size="4" color="white">C</a></font></td>
    </tr>
    <tr>
        <td><a href="" target="_blank"><font size="4" color="white">A</a></font></td>
    </tr>
    <tr>
        <td><a href="" target="_blank"><font size="4" color="white">B</a></font></td>
    </tr>
</table>

You are missing the == operator to compare.

<input type=button id="links" text="Switch Mode" onclick="toggle_visibility()" class="" value="Check Link">
<br>
<a id='not_null' href=""></a>

function toggle_visibility() {    
         var x = document.getElementById('not_null');
         link = x.getAttribute("href");

         if (link =="")//Here you are missing
         {           
         alert("true! This is null");
         }      

  }

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