Monday, March 5, 2012

jQuery: myURL Extension (How to get that pesky site URL on demand/dynamically)

UPDATE: Now includes Parameter support for making site links like

As Seen in jsFiddle Example here:

I would also like to further reiterate that I do appreciate your input, good, bad, or indifferent!
     Let me first say, this is not my usual style blog in that I'm not going to explain this extension much today (perhaps another blog). This is simply to show off a fancy extension/plug-in I've made for jQuery that helps to retrieve your site URL (with parameters!) on demand.

     I find this feature most often useful when doing a lot of Ajax to PHP work where i'm needing to grab specific controllers or controller functions at specific moments. Usually in simple things like loading grids or collecting data for a live change of some sort.

     Anyway, on with the show! I'm going to start this by showing you how easy this function is to use. Then I'll give you all of it's possible uses. Finally, if you're just here for the extension, you'll find it at the bottom of this post.
     The Ease of Use!      OMG! That was so ... easy? Could it really be? And keep in mind, while there are many alternatives out there for doing this, most require you to check the browser version as different browsers have different methods for fetching the key info needed to build your URL string. I already did the work for you!

     Now let's look at many of the possible ways to use this so you can maximize your coding experience without a lot of horrendous thinking (ow my head!).

     Well you saw how to get the basic URL of your sight, but what if you need that ever famous `index.php` page? Try this:
     Now let's try something a little harder. Perhaps you need a image from your image folder that cannot be reached via the index.php but rather by your base URL plus images directory. In other words, your images folder would be like ``. LoL, all too easy for the myURL func!
     Wheh! We're almost to the end (finally, this is taking forever :P). You're final quest is to put it all together and grab a directory with content via the index.php add-in!
     Well, you're almost ready for the actual code, though I should throw just two more piece of info at ya. First, if you really don't like breaking it all down in multiple string parameters, then just use one! See:
     WAIT! What about this parameter update I came here for?      Perhaps you need your site link with data sent to a REST control or a youtube style player is on your site using your own videos, or whatever your purpose! Now simply include a JSON style object as the LAST parameter in your call to myURL and watch the magic happen!
     And finally, for you control freaks out there. I did ensure to include a config that can be run just after you add the plugin. These config options will allow you to set a main directory and sub-directory for both local and on-line use. Keep in mind though, these directories are added before index.php because this is ideal if you are working from a local host with multiple domain directories or even an online sand-box of the same setup. You can also change this at anytime, but every call you make to myURL after you change the config will be reflected.
FINALLY, the Plug-In!

1 comment:

  1. Hi JD,

    Nice utility, but I had a clarification what if the URL string also required a port number example