/ / Lavorare con hashtag - javascript, jquery, url, tag, hashtag

Lavorare con hashtag - javascript, jquery, url, tag, hashtag

Pensiamo che il nostro URL assomigli

www.domain.com # # CourseID LessonID

Quello che sto cercando di fare è, quando:

  • CourseDivButton cliccato se l'url corrente
    • Non ha hashtag (www.domain.com), quindi basta aggiungere $(this).data("id") come hashtag (www.domain.com #CourseID)
    • Contiene solo il primo hashtag - CourseID (www.domain.com #CourseID) quindi modificalo (www.domain.com #NEWCourseID)
    • Contiene 2 hashtag - CourseID e LessonID (www.domain.com # # CourseID LessonID) quindi elimina il secondo e modifica per primo (www.domain.com #NEWCourseID)
  • LessonDivButton cliccato se l'url corrente
    • Contiene solo il primo hashtag - CourseID (www.domain.com #CourseID) quindi aggiungi il secondo (www.domain.com # # CourseID LessonID)
    • Contiene 2 hashtag - CourseID e LessonID (www.domain.com # # CourseID LessonID) quindi cambia secondo e parti prima così com'è (www.dominio.com) # # CourseID NEWLessonID)

Il mio codice ora appare come sotto. Non riesco a capire come ottenere ciò che voglio.

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;
});

Eventuali suggerimenti?

risposte:

1 per risposta № 1

Invece di usare hashtag, non potresti usare stringhe di query? Ti suggerirei di aggiungerle in questo modo:

www.domain.com?course=CourseID& Lezione =LessonID

Usando la funzione javascript di seguito, puoi prendere le variabili dall'URL in questo modo:

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, " "));
}

Se è necessario creare un nuovo URL con la stringa di query aggiunta, controllalo.