Reputation: 179
The first time my page loads, all content loads correctly. When loading a the same section of the page again using Ajax (to refresh content), the Jquery does not fully load.
Is this because the first time Jquery is activated by 'on page load' or something, so when opened in ajax window - the page hasn't actually reloaded, so Jquery isnt activated?
Here's the code I think causes the issue .. does it just need to activating when opened in an ajax div?
<!-- Once the page is loaded, initalize the plug-in. -->
<script type="text/javascript">
(function ($){
var handler = $('#tiles li');
handler.wookmark({
// Prepare layout options.
autoResize: true, // This will auto-update the layout when the browser window is resized.
container: $('#main'), // Optional, used for some extra CSS styling
offset: 5, // Optional, the distance between grid items
outerOffset: 0, // Optional, the distance to the containers border
itemWidth: 178 // Optional, the width of a grid item
});
// Update the layout.
handler.wookmark();
});
})(jQuery);
</script>
I should mention that the Jquery is being used for styling reasons (it cleverly styles the page content). I would guess that happens when handler.wookmark();
is activated. How can I activate this in the ajax window?
I've been asked to provide my ajax code, so here it is:
<!-- ajax script -->
<script>
window.onload = function () {
var everyone = document.getElementById('everyone'),
favorites = document.getElementById('favorites');
everyone.onclick = function() {
loadXMLDoc('indexEveryone');
var otherClasses = favorites.className;
if (otherClasses.contains("Active")) {
everyone.className = 'filterOptionActive';
favorites.className = 'filterOption';
}
}
favorites.onclick = function() {
loadXMLDoc('indexFav');
var otherClasses = everyone.className;
if (otherClasses.contains("Active")) {
favorites.className = 'filterOptionActive';
everyone.className = 'filterOption';
}
}
function loadXMLDoc(pageName)
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("leftCont").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","../home/" + pageName + ".php",true);
xmlhttp.send();
}
}
</script>
<!-- ends ajax script -->
Upvotes: 3
Views: 1042
Reputation: 179
I am a freaking genius, I solved my own problem! (This has never happened before :D)
I had to add the javascript into my ajax coding so it would be re-read once the ajax refreshed.
ANSWER:
<!-- ajax script -->
<script>
window.onload = function () {
var everyone = document.getElementById('everyone'),
favorites = document.getElementById('favorites');
everyone.onclick = function() {
loadXMLDoc('indexEveryone');
var otherClasses = favorites.className;
if (otherClasses.contains("Active")) {
everyone.className = 'filterOptionActive';
favorites.className = 'filterOption';
}
}
favorites.onclick = function() {
loadXMLDoc('indexFav');
var otherClasses = everyone.className;
if (otherClasses.contains("Active")) {
favorites.className = 'filterOptionActive';
everyone.className = 'filterOption';
}
}
function loadXMLDoc(pageName)
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("leftCont").innerHTML=xmlhttp.responseText;
// this is the content that needed adding!
(function ($){
var handler = $('#tiles li');
handler.wookmark({
// Prepare layout options.
autoResize: true, // This will auto-update the layout when the browser window is resized.
container: $('#main'), // Optional, used for some extra CSS styling
offset: 5, // Optional, the distance between grid items
outerOffset: 0, // Optional, the distance to the containers border
itemWidth: 178 // Optional, the width of a grid item
});
// Capture clicks on grid items.
handler.click(function(){
// Randomize the height of the clicked item.
var newHeight = $('img', this).height() + Math.round(Math.random() * 300 + 30);
$(this).css('height', newHeight+'px');
// Update the layout.
handler.wookmark();
});
})(jQuery);
}
}
xmlhttp.open("GET","../home/" + pageName + ".php",true);
xmlhttp.send();
}
}
</script>
<!-- ends ajax script -->
Upvotes: 2
Reputation: 3690
If i understood it right, you need to perform your code also in the:
...
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("leftCont").innerHTML=xmlhttp.responseText;
//Here
handler.wookmark()
}
...
If there is error with handler in the upper code, then make variable global: in your code
(function ($){
var handler = $('#tiles li');
window.handler = handler
and edit first code in my answer:
//Here
window.handler.wookmark()
Upvotes: 0