I’m using a single js file for multiple php pages. At the end of each php I create a javascript variable to identify it, like this:
<div>
.
.
.
</div>
<script>
var idp = 1;
</script>
</body>
</html>
Then in my js file I have:
$(document).ready(function() {
.
.
.
if (idp == 1) {
FunctionWithTheCodeForPage1();
}
if (idp == 2) {
FunctionWithTheCodeForPage2();
}
});
It works fine on Chrome and on Firefox but in IE9 sometimes it does not seems to get the value of idp variable and sometimes it does not execute some jquery code I have before the “if” statements, seems like the problem is the $(document).ready(); because I have set the FunctionWithTheCodeForPage1(); directly inside the php for page 1 and it works, but the rest of the code inside $(document).ready(); does not.
This are the libreries I’m using:
<script type='text/javascript' src="Scripts/Popcalendar.js"></script>
<script type='text/javascript' src='Scripts/jquery-latest.js'></script>
<script type='text/javascript' src='Scripts/jquery.lightbox_me.js'></script>
<script type='text/javascript' src='Scripts/MyJS.js'></script>
This problem does not happens all the time, sometimes the page loads right and sometimes gets this error, but mostly with the error.
Are there compatibility problems with IE9? What am I missing? Thanks
Since you have several functions ready, just pass them directly to jQuery instead of taking a detour over some ID variable.
If you pass a function to jQuery, it will be executed as soon as the page is ready. Normally this is done using an anonymous function, like this:
but you can also just pass an existing function that you defined earlier.
You can also define a unique ID for the bodies and have a single function:
This way you would not have to add any extra script to your pages.