Skip to content
This repository
Browse code

More elegant fix for #1990

Re-use $.mobile.allowCrossDomainPages & call deferred.reject to notify caller of failure
  • Loading branch information...
commit 2deeee14d71ed5c93f6c2317e5b6077ac379a85c 1 parent 8117f12
Ghislain Seguin authored June 30, 2011
3  js/jquery.mobile.core.js
@@ -33,9 +33,6 @@
33 33
 		// Automatically handle clicks and form submissions through Ajax, when same-domain
34 34
 		ajaxEnabled: true,
35 35
 
36  
-		// Allow cross domain XHR, use at your own risks
37  
-		ajaxCrossDomainEnabled: false,
38  
-
39 36
 		// Automatically load and show pages based on location.hash
40 37
 		hashListeningEnabled: true,
41 38
 
181  js/jquery.mobile.navigation.js
@@ -612,27 +612,19 @@
612 612
 			$.mobile.showPageLoadingMsg();
613 613
 		}
614 614
 
615  
-		$.ajaxTransport( "+*", function( s ) {
616  
-			if ( s.crossDomain && !$.mobile.ajaxCrossDomainEnabled ) {
617  
-				return {
618  
-					send: function() {
619  
-						$.error( "Cross domain .ajax() calls are disabled." );
620  
-					},
621  
-					abort: $.noop
622  
-				};
623  
-			}
624  
-		});
625  
-		
626  
-		// Load the new page.
627  
-		$.ajax({
628  
-			url: fileUrl,
629  
-			type: settings.type,
630  
-			data: settings.data,
631  
-			dataType: "html",
632  
-			success: function( html ) {
633  
-				//pre-parse html to check for a data-url,
634  
-				//use it as the new fileUrl, base path, etc
635  
-				var all = $( "<div></div>" ),
  615
+		if ( !( $.mobile.allowCrossDomainPages || path.isSameDomain( documentUrl, absUrl ) ) ) {
  616
+			deferred.reject( absUrl, options );
  617
+		} else {
  618
+			// Load the new page.
  619
+			$.ajax({
  620
+				url: fileUrl,
  621
+				type: settings.type,
  622
+				data: settings.data,
  623
+				dataType: "html",
  624
+				success: function( html ) {
  625
+					//pre-parse html to check for a data-url,
  626
+					//use it as the new fileUrl, base path, etc
  627
+					var all = $( "<div></div>" ),
636 628
 
637 629
 						//page title regexp
638 630
 						newPageTitle = html.match( /<title[^>]*>([^<]*)/ ) && RegExp.$1,
@@ -642,90 +634,91 @@
642 634
 						dataUrlRegex = new RegExp( "\\bdata-" + $.mobile.ns + "url=[\"']?([^\"'>]*)[\"']?" );
643 635
 
644 636
 
645  
-				// data-url must be provided for the base tag so resource requests can be directed to the
646  
-				// correct url. loading into a temprorary element makes these requests immediately
647  
-				if( pageElemRegex.test( html )
648  
-						&& RegExp.$1
649  
-						&& dataUrlRegex.test( RegExp.$1 )
650  
-						&& RegExp.$1 ) {
651  
-					url = fileUrl = path.getFilePath( RegExp.$1 );
652  
-				}
  637
+					// data-url must be provided for the base tag so resource requests can be directed to the
  638
+					// correct url. loading into a temprorary element makes these requests immediately
  639
+					if( pageElemRegex.test( html )
  640
+							&& RegExp.$1
  641
+							&& dataUrlRegex.test( RegExp.$1 )
  642
+							&& RegExp.$1 ) {
  643
+						url = fileUrl = path.getFilePath( RegExp.$1 );
  644
+					}
653 645
 
654  
-				if ( base ) {
655  
-					base.set( fileUrl );
656  
-				}
  646
+					if ( base ) {
  647
+						base.set( fileUrl );
  648
+					}
657 649
 
658  
-				//workaround to allow scripts to execute when included in page divs
659  
-				all.get( 0 ).innerHTML = html;
660  
-				page = all.find( ":jqmData(role='page'), :jqmData(role='dialog')" ).first();
  650
+					//workaround to allow scripts to execute when included in page divs
  651
+					all.get( 0 ).innerHTML = html;
  652
+					page = all.find( ":jqmData(role='page'), :jqmData(role='dialog')" ).first();
661 653
 
662  
-				if ( newPageTitle && !page.jqmData( "title" ) ) {
663  
-					page.jqmData( "title", newPageTitle );
664  
-				}
  654
+					if ( newPageTitle && !page.jqmData( "title" ) ) {
  655
+						page.jqmData( "title", newPageTitle );
  656
+					}
665 657
 
666  
-				//rewrite src and href attrs to use a base url
667  
-				if( !$.support.dynamicBaseTag ) {
668  
-					var newPath = path.get( fileUrl );
669  
-					page.find( "[src], link[href], a[rel='external'], :jqmData(ajax='false'), a[target]" ).each(function() {
670  
-						var thisAttr = $( this ).is( '[href]' ) ? 'href' :
671  
-								$(this).is('[src]') ? 'src' : 'action',
672  
-							thisUrl = $( this ).attr( thisAttr );
673  
-
674  
-						// XXX_jblas: We need to fix this so that it removes the document
675  
-						//            base URL, and then prepends with the new page URL.
676  
-						//if full path exists and is same, chop it - helps IE out
677  
-						thisUrl = thisUrl.replace( location.protocol + '//' + location.host + location.pathname, '' );
678  
-
679  
-						if( !/^(\w+:|#|\/)/.test( thisUrl ) ) {
680  
-							$( this ).attr( thisAttr, newPath + thisUrl );
681  
-						}
682  
-					});
683  
-				}
  658
+					//rewrite src and href attrs to use a base url
  659
+					if( !$.support.dynamicBaseTag ) {
  660
+						var newPath = path.get( fileUrl );
  661
+						page.find( "[src], link[href], a[rel='external'], :jqmData(ajax='false'), a[target]" ).each(function() {
  662
+							var thisAttr = $( this ).is( '[href]' ) ? 'href' :
  663
+									$(this).is('[src]') ? 'src' : 'action',
  664
+								thisUrl = $( this ).attr( thisAttr );
  665
+
  666
+							// XXX_jblas: We need to fix this so that it removes the document
  667
+							//            base URL, and then prepends with the new page URL.
  668
+							//if full path exists and is same, chop it - helps IE out
  669
+							thisUrl = thisUrl.replace( location.protocol + '//' + location.host + location.pathname, '' );
  670
+
  671
+							if( !/^(\w+:|#|\/)/.test( thisUrl ) ) {
  672
+								$( this ).attr( thisAttr, newPath + thisUrl );
  673
+							}
  674
+						});
  675
+					}
684 676
 
685  
-				//append to page and enhance
686  
-				page
687  
-					.attr( "data-" + $.mobile.ns + "url", path.convertUrlToDataUrl( fileUrl ) )
688  
-					.appendTo( settings.pageContainer );
  677
+					//append to page and enhance
  678
+					page
  679
+						.attr( "data-" + $.mobile.ns + "url", path.convertUrlToDataUrl( fileUrl ) )
  680
+						.appendTo( settings.pageContainer );
689 681
 
690  
-				enhancePage( page, settings.role );
  682
+					enhancePage( page, settings.role );
691 683
 
692  
-				// Enhancing the page may result in new dialogs/sub pages being inserted
693  
-				// into the DOM. If the original absUrl refers to a sub-page, that is the
694  
-				// real page we are interested in.
695  
-				if ( absUrl.indexOf( "&" + $.mobile.subPageUrlKey ) > -1 ) {
696  
-					page = settings.pageContainer.children( ":jqmData(url='" + dataUrl + "')" );
697  
-				}
  684
+					// Enhancing the page may result in new dialogs/sub pages being inserted
  685
+					// into the DOM. If the original absUrl refers to a sub-page, that is the
  686
+					// real page we are interested in.
  687
+					if ( absUrl.indexOf( "&" + $.mobile.subPageUrlKey ) > -1 ) {
  688
+						page = settings.pageContainer.children( ":jqmData(url='" + dataUrl + "')" );
  689
+					}
698 690
 
699  
-				// Remove loading message.
700  
-				if ( settings.showLoadMsg ) {
701  
-					$.mobile.hidePageLoadingMsg();
702  
-				}
  691
+					// Remove loading message.
  692
+					if ( settings.showLoadMsg ) {
  693
+						$.mobile.hidePageLoadingMsg();
  694
+					}
703 695
 
704  
-				deferred.resolve( absUrl, options, page, dupCachedPage );
705  
-			},
706  
-			error: function() {
707  
-				//set base back to current path
708  
-				if( base ) {
709  
-					base.set( path.get() );
710  
-				}
  696
+					deferred.resolve( absUrl, options, page, dupCachedPage );
  697
+				},
  698
+				error: function() {
  699
+					//set base back to current path
  700
+					if( base ) {
  701
+						base.set( path.get() );
  702
+					}
711 703
 
712  
-				// Remove loading message.
713  
-				if ( settings.showLoadMsg ) {
714  
-					$.mobile.hidePageLoadingMsg();
715  
-
716  
-					//show error message
717  
-					$( "<div class='ui-loader ui-overlay-shadow ui-body-e ui-corner-all'><h1>"+ $.mobile.pageLoadErrorMessage +"</h1></div>" )
718  
-						.css({ "display": "block", "opacity": 0.96, "top": $window.scrollTop() + 100 })
719  
-						.appendTo( settings.pageContainer )
720  
-						.delay( 800 )
721  
-						.fadeOut( 400, function() {
722  
-							$( this ).remove();
723  
-						});
724  
-				}
  704
+					// Remove loading message.
  705
+					if ( settings.showLoadMsg ) {
  706
+						$.mobile.hidePageLoadingMsg();
  707
+
  708
+						//show error message
  709
+						$( "<div class='ui-loader ui-overlay-shadow ui-body-e ui-corner-all'><h1>"+ $.mobile.pageLoadErrorMessage +"</h1></div>" )
  710
+							.css({ "display": "block", "opacity": 0.96, "top": $window.scrollTop() + 100 })
  711
+							.appendTo( settings.pageContainer )
  712
+							.delay( 800 )
  713
+							.fadeOut( 400, function() {
  714
+								$( this ).remove();
  715
+							});
  716
+					}
725 717
 
726  
-				deferred.reject( absUrl, options );
727  
-			}
728  
-		});
  718
+					deferred.reject( absUrl, options );
  719
+				}
  720
+			});
  721
+		}
729 722
 
730 723
 		return deferred.promise();
731 724
 	};

0 notes on commit 2deeee1

Please sign in to comment.
Something went wrong with that request. Please try again.