Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 2nd, 2012  |  syntax: None  |  size: 3.95 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. shadowbox stops working after jquery function call
  2. <link href="CSS/main.css" rel="stylesheet" type="text/css" />
  3. <script type="text/javascript"  src="shadowbox-3.0.3/shadowbox.js"></script>
  4. <script type="text/javascript">
  5. Shadowbox.init();
  6. </script>
  7.  
  8. <p id="compas"><a href="images/coverage.jpg" rel="shadowbox" title="Coverage map"></a></p>
  9.        
  10. Shadowbox.clearCache();
  11. Shadowbox.setup();
  12.        
  13. <base href="http://62.162.170.125/"/>
  14.        
  15. <div id="menu">
  16.   <ul>
  17.     <li><a id="menu-home" href="index.html" rel="http://jfcoder.com/test/homecontent.html">Home</a></li>
  18.     <li><a id="menu-services" href="services.html" rel="http://jfcoder.com/test/servicescontent.html">Services</a></li>
  19.     <li><a id="menu-tour" href="tour.html" rel="http://jfcoder.com/test/tourcontent.html">Tour</a></li>
  20.     <li><a id="menulogin" href="login.html">Login</a></li>
  21.   </ul>
  22. </div>
  23.        
  24. var boxInitialize = function(){
  25.     try {
  26.         if (!Shadowbox.initialized) {
  27.             Shadowbox.init();
  28.             Shadowbox.initialized = true;
  29.         } else {
  30.             Shadowbox.clearCache();
  31.             Shadowbox.setup();
  32.         }
  33.     } catch(e) {
  34.         try {
  35.             Shadowbox.init();
  36.         } catch(e) {};
  37.     }
  38. };
  39.        
  40. boxInitialize();
  41.        
  42. window.onload = boxInitialize; // Note, no () at the end, which execute the function
  43.        
  44. var $ = jQuery,
  45.     $content = jQuery("#content"), // This is "caching" the jQuery selected result
  46.     view = '',
  47.     detectcachedview = '',
  48.     $fragment,
  49.     s = Object.prototype.toString,
  50.     init;
  51.        
  52. var loadCallback = function(response, status, xhr){
  53.     if (init != '' && s.call(init) == '[object Function]') {
  54.         boxInitialize();
  55.     }
  56.  
  57.     if (xhr.success()
  58.           && view != ''
  59.             && typeof view == 'string'
  60.               && view.length > 1) {
  61.         $fragment = $content.clone(true, true);
  62.         cacheContent(view, $fragment);
  63.     }
  64. };
  65.        
  66. var cacheContent = function(key, $data){
  67.     if (typeof key == 'string'
  68.           && key.length > 1
  69.             && $data instanceof jQuery) {
  70.         $content.data(key, $data.html());
  71.         $content.data(detectcachedview, true);
  72.     }
  73. };
  74.        
  75. var setContent = function(html){
  76.     $content.empty().html(html);
  77.  
  78.     if (init != '' && s.call(init) == '[object Function]') {
  79.         boxInitialize();
  80.     }
  81. };
  82.        
  83. var menuLoadContent = function(){
  84.     // This is where I cancel the request; we're going to show the same thing
  85.     // again, so why not just cancel?
  86.     if (view == this.id || !this.rel) {
  87.         return false;
  88.     }
  89.  
  90.     // I use this in setContent() and loadCallback() functions to detect if
  91.     // the Shadowbox needs to be cleared and re-setup. This and code below
  92.     // resolve the issue you were having with the compass functionality.
  93.     init = this.id == 'menu-home' ? boxInitialize : '';
  94.     view = this.id;
  95.     detectcachedview = "__" + view;
  96.  
  97.     // This is what blocks the superfluous $.load() calls for content that's
  98.     // already been cached.
  99.     if ($content.data(detectcachedview) === true) {
  100.         setContent($content.data(view));
  101.         return false;
  102.     }
  103.  
  104.     // Now I have this in two different spots; there's also one up in
  105.     // loadCallback(). Why? Because I want to cache the content that
  106.     // loaded on the initial page view, so if you try to go back to
  107.     // it, you'll just pickup what was sent with the full document.
  108.     // Also note I'm cloning $content, and then get it's .html()
  109.     // in cacheContent().
  110.     $fragment = $content.clone(true, true);
  111.     cacheContent(view, $fragment);
  112.  
  113.     // See how I use the loadCallback as a function reference, and omit
  114.     // the () so it's not called immediately?
  115.     $content.load(this.rel, loadCallback);
  116.  
  117.     // These return false's in this function block the link from navigating
  118.     // to it's href URL.
  119.     return false;
  120. };
  121.        
  122. jQuery("#menu a[rel]").click(menuLoadContent);
  123.        
  124. $(".content").load('index.html?contentonly=true');
  125.        
  126. var $content = $('.content');
  127. $content.find('.content-view').hide();
  128. $content.find('#services-content').show();