Can a page with different content be displayed if the hash of the URL changes?

How can a page display different content based on a URL hash?

I'm not talking about scrolling the browser to display the anchored section, but something like JavaScript responding to this hash and loading other content through AJAX.

Is this common or even likely?

+3
source share
7 answers

Oh yes - it is becoming a common template for managing persistence of page-status-URLs when content is managed by AJAX.

Javascript window.location.hash. ,

  • /
  • AJAX
  • ..

, DHTML.

+2

. Youtube .

EDIT: , , , , javascript , . . Youtube, .

+2

,

, , ,

// test all possible places hash could be on different browsers
if(window.location.hash){
   hash = window.location.hash;
} else if (document.location.hash){
   hash = document.location.hash;
} else if (location.hash){
   hash = location.hash;
}

// some browsers start the hash with #, remove it for consistency
if(hash.substring(0,1) == '#'){
    hash = hash.substring(1,hash.length);
}

-, , .

: http://www.example.com#pageA

if(hash = 'pageA'){
  document.getElementById('mainContentDiv').innerHTML = '<p> content for the page displayed when the hash sais pageA</p>';
}
+2

Sammy - javascript, .

+2

JavaScript URL-, , , - URL-.

- , , , .

, , TiddlyWiki, .

+1

AJAX- (, Gmail) AJAXy, - . , . - URL , window.location.hash - - Javascript, .

jQuery, : /, history.

+1

, :

, , , , , hashchange ajax, :

+1

Source: https://habr.com/ru/post/1709616/


All Articles