Q:

Working with hashtags

Lets think that our URL look like
www.domain.com #CourseID#LessonID
What I'm trying to do is, when:

CourseDivButton clicked if current url

Has no hashtag (www.domain.com) then just add $(this).data("id") as hashtag (www.domain.com #CourseID)
Contains only first hashtag - CourseID (www.domain.com #CourseID) then change it (www.domain.com #NEWCourseID)
Contains 2 hashtags - CourseID and LessonID (www.domain.com #CourseID#LessonID) then delete second and change first (www.domain.com #NEWCourseID)

LessonDivButton clicked if current url

Contains only first hashtag - CourseID (www.domain.com #CourseID) then add second (www.domain.com #CourseID#LessonID)
Contains 2 hashtags - CourseID and LessonID (www.domain.com #CourseID#LessonID) then change second and leave first as it is (www.domain.com #CourseID#NEWLessonID)

My code now looks like below. Can't figure out how to achieve what I want.
CourseDivButton.click(function(){
    var id=$(this).data("id");
    LoadLessons(id);
    window.location.hash = id;
});

LessonDivButton.live("click", function(){
    var id=$(this).data("id");
    LoadQuestions(id);
    window.location.hash = id;
});

Any suggestions?

A:

Instead of using hashtags, couldn't you use query strings? I would suggest you add them like this:
www.domain.com?course=CourseID&lesson=LessonID
Using the javascript function below, you can grab the variables from the URL like so:
var courseID = urlParam('course');
var lessonID = urlParam('lesson');

function urlParam(name) {
    /// <summary>
    /// Gets the value of a parameter located within the url.
    /// </summary>
    /// <param name="name">The paramter to lookup.</param>
    /// <returns type="string">The value of the requested parameter, or undefined if the parameter is not present.</returns>
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null)
        return undefined;
    else
        return decodeURIComponent(results[1].replace(/\+/g, " "));
}

If you need to build a new URL with the query string added, check this out.

