• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 439
  • Last Modified:

Jquery not working when viewed via an Ajax frame

I am creating a site using Ajax to load pages within a parent page (much like an iframe). Some pages have Jquery elements that are not working.

For instance, if you go here...http://www.riad-hikaya.com/ajax/hometest.php and click on 'Riad' at the top you'll see that you just see a bunch of thumbs whereas the actual page here shows you how it's supposed to look http://www.riad-hikaya.com/ajax/riad.html

Another Jquery function works fine on another page (the home page) it's just the gallery function on the 'Riad' page.

I have attached the whole code for the Ajax holder page which has all the javascript links on there for the content pages.

So to recap http://www.riad-hikaya.com/ajax/riad.html looks fine on it's own but appears as http://www.riad-hikaya.com/ajax/hometest.php (click on 'Riad') in the menu) when using an Ajax frame.

How the page should lookHow the page should look
How the page looks when viewing through an Ajax frame How the page looks when viewing through an Ajax frame
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Riad Hikaya</title>

<link href="css/ria.css" rel="stylesheet" type="text/css" />

<link href="galleria.classic.css" rel="stylesheet" type="text/css" />

<script type="text/javascript" src="js/jquery-1.2.6.min.js"></script>


<script type="text/javascript" src="js/ajax.js"></script>

<script type="text/javascript" src="js/slider.js"></script>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js"></script>

 <script src="js/galleria-1.2.5.js"></script>

<script type="text/javascript">


function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
</script>






</head>

<body onload="MM_preloadImages('images/but-gallery-ovr.png','images/but-home-ovr.png','images/but-riad-ovr.png','images/but-dining-ovr.png','images/but-spa-ovr.png','images/but-services-ovr.png','images/but-rates-ovr.png','images/but-location-ovr.png','images/but-contact-ovr.png')">

<table width="1048" border="0" align="center" cellpadding="0" cellspacing="0" class="top40">
  <tr>
    <td colspan="2" align="center" valign="top"><div id="leftcolumn">
      <table width="100" border="0" cellpadding="0" cellspacing="0" class="menu">
        <tr>
          <td><a href="javascript:ajaxpage('welcome.html', 'rightcolumn');" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Home','','images/but-home-ovr.png',1)"><img src="images/but-home.png" alt="Home" name="Home" width="80" height="27" border="0" id="Home" /></a></td>
          <td><a href="javascript:ajaxpage('riad.html', 'rightcolumn');" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Riad','','images/but-riad-ovr.png',1)"><img src="images/but-riad.png" alt="Riad" name="Riad" width="116" height="27" border="0" id="Riad" /></a></td>
          <td><a href="javascript:ajaxpage('rooms.html', 'rightcolumn');" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Riad','','images/but-rooms-ovr.png',1)"><img src="images/but-rooms.png" alt="Riad" name="Riad" width="94" height="27" border="0" id="Riad" /></a></td>
          <td><a href="dining.html" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Dining','','images/but-dining-ovr.png',1)"><img src="images/but-dining.png" alt="Dining" name="Dining" width="98" height="27" border="0" id="Dining" /></a></td>
          <td><a href="spa.html" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Spa','','images/but-spa-ovr.png',1)"><img src="images/but-spa.png" alt="Spa" name="Spa" width="62" height="27" border="0" id="Spa" /></a></td>
          <td><a href="services.html" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Services','','images/but-services-ovr.png',1)"><img src="images/but-services.png" alt="Services" name="Services" width="110" height="27" border="0" id="Services" /></a></td>
          <td><a href="rates.html" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Rates','','images/but-rates-ovr.png',1)"><img src="images/but-rates.png" alt="Rates" name="Rates" width="78" height="27" border="0" id="Rates" /></a></td>
          <td><a href="location.html" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Location','','images/but-location-ovr.png',1)"><img src="images/but-location.png" alt="Location" name="Location" width="122" height="27" border="0" id="Location" /></a></td>
          <td><a href="contact.php" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Contact','','images/but-contact-ovr.png',1)"><img src="images/but-contact.png" alt="Contact" name="Contact" width="120" height="27" border="0" id="Contact" /></a></td>
        </tr>
      </table>
    </div></td>
  </tr>
  <tr>
    <td width="304" valign="top"><div id="rightcolumn">
      <div id="rightcolumn2">
        <table width="1048" border="0" align="center" cellpadding="0" cellspacing="0" class="display">
          <tr>
            <td width="304" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td><img src="images/riadlogo.jpg" width="303" height="91" alt="Riad hikaya Logo" /></td>
              </tr>
              <tr>
                <td><table width="493" border="0" cellpadding="20" cellspacing="0" class="copybox">
                  <tr>
                    <td valign="top" bgcolor="#B3A582"><h1><img src="images/title-welcome.jpg" width="255" height="52" alt="welcome" /></h1>
                      <p>Riad  Hikaya is a tradtional and  beautifully elegant residence located in the royal quarter of the medina, called the 'kasbah', and only an easy walk from the main square, jemma al fnaa, and the city's other famous landmarks.<br />
                        <br />
                        After three years of meticulous restoration by local craftsmen and artisans, employing both age old techniques and materials, the riad has been transformed into a luxury, boutique hotel.<br />
                        <br />
                        The  harmonious blend of ancient tradition and contemporary comforts, coupled with our emphasis on quality and personal service, will create for you an unforgettable and unique experience.<br />
                        <br />
                        We look forward to welcoming you to Riad Hikaya.</p></td>
                  </tr>
                </table></td>
              </tr>
            </table></td>
            <td width="744" valign="top"><table width="743" border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td></td>
              </tr>
            </table></td>
          </tr>
        </table>
      </div>
 
    </div></td>
    
  </tr>
  <tr>
    <td colspan="2" align="center" class="footer"><table width="1046" border="0" cellpadding="5" cellspacing="0">
      <tr>
        <td align="center" valign="bottom" class="footer"><br />
          Riad Hikaya, 252 Rue Sidi Mansour, Kasbah  Medina, Marrakesh - Tel: +212 123 45 67 89 - Fax: +212 123 45 67 89 - info@riad-hikaya.com</td>
      </tr>
      <tr>
        <td align="center" valign="bottom"></td>
</tr>
      <tr> </tr>
    </table></td>
  </tr>
</table>
<script>

    // Load the classic theme
	Galleria.loadTheme('galleria.classic.min.js');

	// Initialize Galleria
	$('#galleria').galleria();

    </script>
	
</body>
</html>

Open in new window

0
BrighteyesDesign
Asked:
BrighteyesDesign
  • 2
1 Solution
 
Albert Van HalenAnalyst developerCommented:
Use jQuery for loading your content as well.
The trick that jQuery will do is to parse/evaluate scripts within the content loaded via AJAX.

The content which you want to load is the href attribute of the links.

Use this :
$(function() {
    $(".roomboxes a").click(function() { $("#rooms").load(this.href); return false; })
});

Open in new window


The above code will make sure that when DOM is ready the click event is being bound to all of the links whitin the table having the classname 'roomboxes'.

The first link (the image) uses a normal href, the second link (more) uses a javascript:ajaxpage.
Make sure that the second link is like the first link and you're all set.
0
 
BrighteyesDesignAuthor Commented:
Hi Albert,

Thanks for that, could you run that by me again please? Where would I add that code you mention?

Here's a link to the site again http://www.riad-hikaya.com/ajax/home.html
I have now made all the links load into the holder 'right column'

Thanks for your patience!
0
 
Albert Van HalenAnalyst developerCommented:
Hi BrighteyesDesign

You're loading everything via AJAX. No problem of course but the way you do it scripts are never parsed / executed when loaded via the existing method.
Each click-able link in your site has the href set to javascript:ajaxpage(). (I guess it's rendered in some serverside code using PHP, ASP, .NET or whatever...)

I would do it slightly different.

Instead of putting javascript:ajaxpage into the href, I would just use the link for the page. For example : href='index2.html' (so without javascript:ajaxpage).
After that I would add a class to each link. For example 'nav'.

Now in the main page I would register a live handler to each element having the classname 'nav' for the click event and use the code I mentioned earlier.

Use the piece of code below in the start page (home) and put it somewhere in script tags at the end of your document but before the closing tag of the body.

You gain at least 2 wins.
 - Your code is unobtrusive, it's kind of ugly that the links you can click have the href set to javascript:ajaxpage.
 - If a visitor doesn't have a javascript enabled webbrowser, it will function anyhow.

And that's it! The live handler will take care of the click event and will make sure the if a link is clicked with classname 'nav' the event will be executed (even with elements that are not in your document yet!).
http://api.jquery.com/live/

Good luck

<body>
...
<script type="text/javascript">
$(function() {
    $("a.nav").live("click", function() {
       $("#rightcolumn").load(this.href);
       return false;
    })
});
</script>
</body>

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now