Add SP.ClientContext.get_current().get_url(); as the initial attempt to discover the current site URL. This is defined in SP2010, but not in SP2007.
Comments: Improved context senseing here, which is essentially the same idea: https://spservices.codeplex.com/workitem/10136
Comments: Improved context senseing here, which is essentially the same idea: https://spservices.codeplex.com/workitem/10136