post

Defer parsing of javascript to improve performance in wordpress

Nothing for introduction, hehe! A very small snippet which will help you to defer parsing all javascript files except jquery in wordpress. Here is the code:

You can add those codes in your functions.php in the theme, if you think your theme won’t be changed. Otherwise mu-plugins is the best solution. To use mu-plugins, go to /wp-content/ and find the folder with name ‘mu-plugins’. If there is no folder in that name, then create a folder, name it ‘mu-plugins’, create a file inside that, give any name you like and paste the code in there. You don’t need to activate that plugin. Mu-plugins means must use plugins, so it will be activated automatically always. If you use mu-plugins then add a php start tag at the beginning of the code.

post

How to get start date of a week in JavaScript

This is little tricky to get the start date or/and end date of a week. Also, there is no standard day that should be a REAL start day in a week. In my example, I assume that the start day is Monday. Also, I will show you how you can adjust it to fit your needs. So, you can make it as Saturday or Sunday or whatever.

In my example, suppose I want to make a title like “Accepting Bookings For the Week of START_DATE to END_DATE”. So, within this week, it should be same. But in next week, it should be changed automatically.

Well, it’s not hard, let’s look at the code:
[html]

Accepting Bookings For the Week of Feb 23rd to Feb 23rd

//
[/html]

There is some vita points. First month array, I needed to use that to print the month name as JavaScript has no function to print month name. So, when we will get the month number (0, 1, 2 etc) we will print the same index of month array. Note that getMonth() function returns the month number based on zero, so January would be 0, February would be 1 and so on.

Next one is:
[javascript]
startDate = d.getDate() – day + (day == 0 ? -6:1)
[/javascript]

This is where you need to edit if you change the start day. Change -6 to something between -1 to -7 and you will see the result.

And then the following line set the end date:
[javascript]
endDate = startDate + 6;
[/javascript]

If you want to make it for next week, then change this line
[javascript]
startDate = d.getDate() – day + (day == 0 ? -6:1)
[/javascript]

to

[javascript]
startDate = (d.getDate() + 7) – day + (day == 0 ? -6:1)
[/javascript]

And finally, I set the correct value in correct place:
[javascript]
document.getElementById(‘ds’).innerHTML = month[correctStartDate.getMonth()] + ” ” + correctStartDate.getDate();
document.getElementById(‘dl’).innerHTML = month[correctEndDate.getMonth()] + ” ” + correctEndDate.getDate();
[/javascript]

:)

post

Redirect to another page when back button is pressed in JavaScript

One of the most wanted feature now-a-days is to detect browser back button. Couple of years ago there were lots of script to detect browser’s back button, but browsers are getting smarter day by day, and so are we :)

Sometimes we need to redirect into a different page when user press browser back button. So, we need to detect when the user press back button. Well a developer Brooke Bryan wrote a script for detecting pressing back button. You will found the script here. But it will detect back button and then redirect to old page. I have modified the original code based on his suggestion so that you can set a script to redirect into a different page.

You can get modified script here:
[javascript]
/*!
* Back Button Detection Object V 1.0.1
* www.brookebryan.com/
*
* Copyright 2010, Brooke Bryan
*
* Date: Thu 27 Jan 2011 13:37 GMT
*/

var bajb_backdetect={Version:’1.0.0′,Description:’Back Button Detection’,Browser:{IE:!!(window.attachEvent&&!window.opera),Safari:navigator.userAgent.indexOf(‘Apple’)>-1,Opera:!!window.opera},FrameLoaded:0,FrameTry:0,FrameTimeout:null,OnBack:function(){alert(‘Back Button Clicked’)},BAJBFrame:function(){var BAJBOnBack=document.getElementById(‘BAJBOnBack’);if(bajb_backdetect.FrameLoaded>1){if(bajb_backdetect.FrameLoaded==2){bajb_backdetect.OnBack();}}bajb_backdetect.FrameLoaded++;if(bajb_backdetect.FrameLoaded==1){if(bajb_backdetect.Browser.IE){bajb_backdetect.SetupFrames()}else{bajb_backdetect.FrameTimeout=setTimeout(“bajb_backdetect.SetupFrames();”,700)}}},SetupFrames:function(){clearTimeout(bajb_backdetect.FrameTimeout);var BBiFrame=document.getElementById(‘BAJBOnBack’);var checkVar=BBiFrame.src.substr(-11,11);if(bajb_backdetect.FrameLoaded==1&&checkVar!=”HistoryLoad”){BBiFrame.src=”blank.html?HistoryLoad”}else{if(bajb_backdetect.FrameTry<2&&checkVar!=”HistoryLoad”){bajb_backdetect.FrameTry++;bajb_backdetect.FrameTimeout=setTimeout(“bajb_backdetect.SetupFrames();”,700)}}},SafariHash:’false’,Safari:function(){if(bajb_backdetect.SafariHash==’false’){if(window.location.hash==’#b’){bajb_backdetect.SafariHash=’true’}else{window.location.hash=’#b’}setTimeout(“bajb_backdetect.Safari();”,100)}else if(bajb_backdetect.SafariHash==’true’){if(window.location.hash==”){bajb_backdetect.SafariHash=’back';bajb_backdetect.OnBack();}else{setTimeout(“bajb_backdetect.Safari();”,100)}}},Initialise:function(){if(bajb_backdetect.Browser.Safari){setTimeout(“bajb_backdetect.Safari();”,600)}else{document.write(‘‘)}}};bajb_backdetect.Initialise();
[/javascript]

Now we need to use it.

Fortunately, you don’t need a second page to check this script. You will use this script in the page from where you need to redirect into a different page when back button is pressed. Logic is, when the page with the script loaded, a hashtag (#b) is automatically added with the URL. So, you can press the back button for the first page (as I said don’t need a second page to test).

I assume you have saved the above script in a file called – backfix.min.js and put it in js folder. So, first include the script into your html head section. And then call the script:

[html]

//
[/html]

And you’re done. You can do anything you want with script within that function. You also can set a message like “Are you sure want to live this page?” with confirm function.

So, Brooke made our life easy, Good bless him :)