lostcoderx lostcoderx - 7 months ago 20
Javascript Question

Run javascript based on url

okay so I need this script

$(document).ready(function() {
$('a[id="twittersocial"]').click(function() {
$(".twittersocial").slideToggle("slow", function() {
$(".twittersocial").load('social/twitter.php?k=<?php echo"$website"; ?>', function() {});
});
});
});


to run when someone goes to the direct url
www.website.com#twittersocial
and when of course when somene click the link
twitter social

How can I achieve this ?

Answer

When the page is loaded, check whether the hash is set and run your code. Put the code in a function so you can call it from both the event handler and at startup.

$(function() {
    function loadTwiterSocial() {
        $(".twittersocial").slideToggle("slow", function() {
            $(".twittersocial").load('social/twitter.php?k=<?php echo"$website"; ?>', function() {});
        });
    }

    if (location.hash == "#twittersocial") {
        loadTwitterSocial();
    }

    $('a#twittersocial').click(loadTwitterSocial);
}