The javascript function does not run when the HTML page loads for the first time. However, if I do a page refresh, the script runs just fine.
Description :
My index page has a button which points to a showdata HTML page. The problem is that when I click the button in the index page, I can see (in the URL) that the showdata.html page is loaded but the JS code does not run (as I cannot see any alerts).
However, if I do a page refresh or hit the URL manually, the JS runs fine and I can see the alert statements.
Tried this on Chrome and Firefox and same results at both the places.
If I am not wrong the $(function() is a shorthand for $(document).ready. In any case, I've tried using both the syntax but the results are same.
The current code for the page is:
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Test</title>
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.3/jquery.mobile-1.4.3.min.js"></script>
<script type="text/javascript">
$(function() { alert("hello"); });
</script>
</head>
<body></body>
</html>
I am unable to see the expected alert box when the page first loads. When I reload the page, the alert comes as expected.
Update:
I cleared the cache but no results.
Also, when I remove the
<script src="http://code.jquery.com/mobile/1.4.3/jquery.mobile-1.4.3.min.js"></script>
tag from the index page where the link to my current page is present, the JS fires correctly on the first load of the showdata.html page. However, this causes loss of styling in the index page.