JS Check the page has no duplicate ID on the implementation code

Source: Internet
Author: User
Sometimes we need to check whether a page with a duplicate ID, the general ID is unique, but also convenient control, then you can refer to the following code

Method One:

Copy Code code as follows:


<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >


<HTML>


<HEAD>


<TITLE> New Document </TITLE>


<script type= "Text/javascript" >


window.onload = function () {


var tags = document.getelementsbytagname ("*"),


count = tags.length, time, ret = {}, id;


time = new Date ();


for (var i = 0; i < count; i++) {


id = tags[i].id;


if (ID) {


if (Ret[id]) {


alert (id + '/n Time: ' + (new Date ()-time);


return;


}else{


Ret[id] = true;


                }


            }


}


alert ("No same ID found");


    }


</script>


</HEAD>


<BODY>


<script type= "Text/javascript" >


(function () {


var html = [], rnd = parseint (Math.random () * 1000);


for (var i = 0; i < 1000; i++) {


Html.push ("<div id= ' a" + i + "' >" + i + "</div>");


        };


//insertion of a random ID in a random position


i = parseint (Math.random () * 1000);


Html[i] = Html[i] + ("<div id= ' a" + rnd + "' >" + rnd + "</div>");


document.write (Html.join (""));


    })();


</script>


</BODY>


</HTML>

Method Two:

Copy Code code as follows:




<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >


<HTML>


<HEAD>


<TITLE> New Document </TITLE>


<script type= "Text/javascript" >


function Creatediv () {


var html = [], id;


for (var i = 0; i < i++) {


id = "divID" + parseint (math.random () * 10000);


Html.push ("<div id= '" + ID + "' >" + ID + "</div>");


        }


Document.body.innerHTML = Html.join ("");


    }


window.onload = function () {


Creatediv ();





var OID = {}, result = {}, arr = [],


tags = document.getelementsbytagname ("*");


for (var i = 0, id; i < tags.length; i++) {


id = tags[i].id;


if (ID) {


Oid[id] = Oid[id]? Oid[id] + 1:1;


if (Oid[id] > 1) {


Result[id] = id + "" + Oid[id];}} for (var o to result) {


Arr.push (Result[o]);


        }


Alert (Arr.join ("T"));


    };


</script>


</HEAD>


<BODY>


</BODY>


</HTML>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.