Skip to content Skip to sidebar Skip to footer

Javascript Code Working In Jsfiddle But Not Working In The Browser

my code is working in jsfiddle but not working in the browser i dont know what i missed here could you please check it and tell me the solution. i google it but i am not getting c

Solution 1:

jsfiddle has wrapped it automatically in the onload event. You haven't done this in your HTML page, so when it runs in the browser you're trying to get hold of the div before it's been rendered, so theDiv is null.

The below should work:

<!doctype html><html><head><scriptsrc="http://code.jquery.com/jquery-latest.min.js"type="text/javascript"></script></head><scripttype="text/javascript">window.onload = function () {
        var random = 0;
        var theDiv = document.getElementById("showVal");
        updateTheDiv(9, 0);
        functionupdateTheDiv(inRange, inMin) {
            random = (Math.random() * inRange + inMin) | 0;
            theDiv.innerHTML = random;
            var nextCall = (Math.random() * 1000) | 0;
            setTimeout(function () {
                updateTheDiv(inRange, inMin);
            }, nextCall);
        }
    }
</script><body><divid="showVal"></div></body></html>

Solution 2:

Try this in your Browser ;)

<!doctype html><html><head><scriptsrc="http://code.jquery.com/jquery-latest.min.js"type="text/javascript"></script></head><body><divid="showVal"></div></body><scripttype="text/javascript">var random = 0;
    var theDiv = document.getElementById("showVal");
    updateTheDiv(9,0);
    functionupdateTheDiv(inRange, inMin) {
    random = (Math.random()*inRange+inMin)|0; 
    theDiv.innerHTML = random;
    var nextCall = (Math.random()*1000)|0;
    setTimeout(function() {
    updateTheDiv(inRange, inMin);
        }, nextCall);
    }
  </script></html>

(I just put the script to the bottom. So the script loads after your element is set.)

Post a Comment for "Javascript Code Working In Jsfiddle But Not Working In The Browser"