Skip to content Skip to sidebar Skip to footer

Jquery Code Only Works On Page Refresh

I have a simple jQuery script that changes a value pulled dynamically like so: (function($) { $(window).load(function () { $(function(){ var inNum = parseF

Solution 1:

Try this:

$(document).ready(function() {
    var inNum = parseFloat($(".value").text().match(/[\d\.\d]+/i)).toFixed(2);
    $('span.value').text(inNum);
});

It will execute the code right away when the DOM is loaded.

Solution 2:

Try to put your code in a function:

functionTestCall()
{
  var inNum = parseFloat($(".value").text().match(/[\d\.\d]+/i)).toFixed(2);
  $('span.value').text(inNum);
}

And call this function on window.onload:

window.onload=TestCall;

This should work in your case.

Solution 3:

I trust my answer comes very late but this goes to those still having this problem. After spending 2 days trying all possible solutions posted on the Internet in vain, the only thing that helped was going the old fashion way

I added the onclick clause on the button.

<a href="#"id="createAccbtn" onclick="handle_creatAccbtn_function()" ....

I know this might not be the best solution but it worked. Got a client off my back and can be looking for better solution without someone breathing down my neck.

Solution 4:

Your function isn't working probably because some resource (script, css or image) still loading, so the load event is only fired when you hit refresh which causes all resources to stop loading. It causes the load event to be fired in the actual page.

You can just use:

(function($) {
    $(function(){
        var inNum = parseFloat($(".value").text().match(/[\d\.\d]+/i)).toFixed(2);
        $('span.value').text(inNum);
    });
}(jQuery));

The jQuery variable still be mapped to $ inside your function and your script will be executed when the DOM is ready (even if some resource still loading).

Solution 5:

I had the same problem, but solved it with this inside the $(document).ready to reload the page only once.

if(!window.location.hash) {
        window.location = window.location + '#loaded';
        window.location.reload();
    }else{
      //Init stuff here
    }

I prefer this solution above adding a plugin and messing up code everywhere. Avoid using location reload(true); 'true' means it will empty the cache and causes double download traffic.

Post a Comment for "Jquery Code Only Works On Page Refresh"