mercredi 6 mai 2015

javascript code disappears on source window after refreshing page in rails 3 using jquery

Here is a unique problem I am not able to find solution to. I write a small black of code given as below.

$(document).ready(function () {
alert("Page loaded");
if (/iPhone|iPad|iPod/i.test(navigator.userAgent)) {
    $(document).on('visibilitychange', function () {
        if (document.hidden == false) {
            console.log("gotcha");
            location.reload();
        }
        console.log(document.hidden, document.visibilityState);
    });
}
});

I was working only for ipad(the bug was device specific) but now I face the issue that this code disappears after refreshing page a few times in all other machines also.

Solutions attempted till now:

  1. I added this within the tag with and without specifying type="text/javascript" in the beginning of the page and in the end as well. Astonishingly this code disappears the same way and few other variables defined for javascript code within the tag remains same and visible.
  2. I added a file separately with this block of code in it and included that file with <%= javascript_include_tag %> in two ways.

    2.1 Simply adding this tag in my separate view file.

    2.2 Adding it using "content_for" helper and thus calling it with yield and specified name in the application.html.erb file.

3.When I add this in my appliaction.js within assets folder it works but I need this specifically for a particular action else it will reduce my site's performance .

I do not understand whether the issue is with the code or what. I will be really thankful if someone provides a required solution to it.

I am using following things in it

ruby 1.9.3

rails 3.2.12

running server with foreman

memcache

Aucun commentaire:

Enregistrer un commentaire