www.webdeveloper.com
Results 1 to 3 of 3

Thread: JavaScript Conflict

  1. #1
    Join Date
    Dec 2012
    Posts
    6

    JavaScript Conflict

    Hi,

    Need some help regarding the google ads code conflict with otherJS. Actually I am using the below JS in my webpage to randomly rotate some content.


    [I][I]<div id = "myDiv0" style="display:none">ABC0</div>
    <div id = "myDiv1" style="display:none">ABC1</div>
    <div id = "myDiv2" style="display:none">ABC2</div>
    <div id = "myDiv3" style="display:none">ABC3</div>
    <div id = "myDiv4" style="display:none">ABC4</div>
    <div id = "myDiv5" style="display:none">ABC5</div>
    <div id = "myDiv6" style="display:none">ABC6</div>

    <script type = "text/javascript">

    var numberOfDivs = 7; // or however many

    window.onload = function() {
    var list = [];
    for (var i = 0; i < numberOfDivs; i++) {
    list [i] = i;
    }
    shuffle(list);
    alert (list); // for testing

    for (var i = 0; i < 4; i++) { // show 4 out of 7
    document.getElementById(["myDiv"+ list[i]]).style.display="block";
    }
    }

    shuffle = function(v){
    for(var j, x, i = v.length; i; j = parseInt(Math.random() * i), x = v[--i], v = v[j], v[j] = x);
    return v;
    }
    </script>



    Now whenever I insert google ads code in my webpage above content got hidden automatically.

    Any Suggestions? Thanks...

  2. #2
    Join Date
    Nov 2010
    Posts
    978
    I think there's something wrong with your shuffle function - the code you posted doesn't run even without the google ads code. Try this one:

    Code:
    function shuffle(array) {
    var n = array.length, k, t;
    if (n == 0) {return false};
    while (--n) {
    k = Math.floor(Math.random() * (n+1));
    t = array[n];
    array[n] = array[k];
    array[k] = t;
    }
    }

  3. #3
    Join Date
    Nov 2010
    Posts
    978
    if you wanted to make it a little more dynamic...
    Code:
    <head>
    <style>
    #shuff > div{
    display:none;
    }
    </style>
    </head>
    <body>
    <div id="shuff">
    <div>ABC0</div>
    <div>ABC1</div>
    <div>ABC2</div>
    <div>ABC3</div>
    <div>ABC4</div>
    <div>ABC5</div>
    <div>ABC6</div>
    </div>
    <script type = "text/javascript">
    var divs=document.getElementById("shuff").getElementsByTagName("div");
    
    var list = [];
    for (var i = 0; i < divs.length; i++) {
    list [i] = i;
    }
    shuffle(list);
    alert (list); // for testing
    
    for (var i = 0; i < 4; i++) { // show 4 out of 7
    divs[list[i]].style.display="block";
    }
    
    
    function shuffle(array) {
    var n = array.length, k, t;
    if (n == 0) {return false};
    while (--n) {
    k = Math.floor(Math.random() * (n+1));
    t = array[n];
    array[n] = array[k];
    array[k] = t;
    }
    }
    </script>
    </body>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles