Page change without reloading page
This commit is contained in:
		
							parent
							
								
									4cd3402f9d
								
							
						
					
					
						commit
						9887857fc9
					
				
					 1 changed files with 20 additions and 7 deletions
				
			
		
							
								
								
									
										27
									
								
								js/main.js
									
										
									
									
									
								
							
							
						
						
									
										27
									
								
								js/main.js
									
										
									
									
									
								
							|  | @ -15,13 +15,20 @@ $(document).ready(() => { | |||
| 
 | ||||
| 
 | ||||
| 	let path = document.location.pathname; | ||||
| 	console.log("pathname: ", path); | ||||
| 
 | ||||
| 	if(path === "/" || path === "" || path === "/index.html"){ | ||||
| 		path = "/home"; | ||||
| 	} | ||||
| 
 | ||||
| 	setPage(path); | ||||
| 
 | ||||
| 	$("nav > a").each((i, elmt) => { | ||||
| 		const href = $(elmt).attr("href"); | ||||
| 		if(href[0] === "/"){ | ||||
| 			$(elmt).bind("click", () => { | ||||
| 				// window.location.pathname = href;
 | ||||
| 				history.pushState(null, null, href); | ||||
| 				setPage(href); | ||||
| 				return false; | ||||
| 			}) | ||||
| 		} | ||||
| 	}) | ||||
| }) | ||||
| 
 | ||||
| 
 | ||||
|  | @ -37,6 +44,10 @@ function setPage(path){ | |||
| 		sanitize: false, | ||||
| 	}; | ||||
| 
 | ||||
| 	if(path === "/" || path === "" || path === "/index.html"){ | ||||
| 		path = "/home"; | ||||
| 	} | ||||
| 
 | ||||
| 	$.ajax("/pages"+path+".md") | ||||
| 		.done((res) => { | ||||
| 			$("#article-container").html(res); | ||||
|  | @ -54,8 +65,10 @@ function setPage(path){ | |||
| 					return res; | ||||
| 				})); | ||||
| 			}); | ||||
| 
 | ||||
| 
 | ||||
| 		}) | ||||
| 		.fail((err) => { | ||||
| 			$("#article-container").html(`<article><h1>Error ${err.status} :'(</h1><p>${err.statusText}<br/>${err.responseText}</p>`); | ||||
| 			console.error(err); | ||||
| 		}); | ||||
| } | ||||
| 
 | ||||
|  |  | |||
		Loading…
	
	Add table
		
		Reference in a new issue