Last active
December 20, 2017 13:31
-
-
Save alenabdula/9954542 to your computer and use it in GitHub Desktop.
Simple AJAX, CSS-Tricks Forum: http://css-tricks.com/forums/topic/how-to-use-ajax/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>Simple AJAX Demo</title> | |
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> | |
</head> | |
<body> | |
<ul class="main-navigation" data-js="navigation"> | |
<!-- links to pages, to be loaded via AJAX --> | |
<li><a href="product-one.html">Product One</a></li> | |
<li><a href="product-two.html">Product Two</a></li> | |
<li><a href="product-tri.html">Product Tri</a></li> | |
</ul> | |
<!-- Placeholder for content to be rendered via AJAX --> | |
<div class="view" data-js="view">Template View</div> | |
</body> | |
<script> | |
(function($){ | |
// On Ready | |
$(document).ready(function(){ | |
var view = $('[data-js="view"]'); | |
var menuList = $('[data-js="navigation"]').find('a'); | |
menuList.on('click', function(event){ | |
// This prevents the browser from going directly to linked pages | |
// instead we let the JavaScript control this interactivity | |
event.preventDefault(); | |
// Our local variable that holds reference to the link | |
// being clicked on, it also gets the URL so we can pass | |
// it to our AJAX request | |
var url = $(this).attr('href'); | |
// Make the AJAX request | |
$.ajax( | |
{ | |
// pass the URL we collected earlier | |
url: url, | |
// we're targeting only content in the body element | |
// since we don't want unnecessary html tags | |
// like html, head, title, etc... | |
// only content from within <body></body> tag! | |
context: document.body, | |
// Set cross-domain policy | |
crossDomain: true, | |
// if request is successful bind the response to our view | |
success: function(response) { | |
// bind the view | |
view.html(response); | |
} | |
} | |
); | |
}); | |
}); | |
// // On Load | |
// $(window).load(function(){ | |
// console.log('load'); | |
// }); | |
// // On Resize | |
// $(window).resize(function(){ | |
// console.log('resize', 'Width: ' + $(window).width()); | |
// }); | |
})(window.jQuery); | |
</script> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks for sharing. i found your website via search engine i also i fons some related information in this website
طراحی وب سایت
طراحی سایت