2012-09-26 45 views
1

我是Jquery的新手,我想讓Jquery代碼獲取當前頁面的url,如果url包含某些字符串,然後加載遠程元素。Jquery根據當前頁面中的字符串加載遠程頁面元素

例如:

我有網頁網址是這樣的:

"http://......./Country/AU/result-search-to-buy" 
"http://......./Country/CA/result-search-to-buy" 
"http://......./Country/UK/result-search-to-buy" 

部分「/國家/ AU」是什麼,我需要確定我要加載的頁面元素,那麼如果「 AU「如果從」/state-loader.html .state-CA「加載」/state-loader.html .state-AU「我加載我有一個內置模塊」{module_pageaddress }「來獲取當前頁面url的值,我只是不知道jquery logi c讓它工作。

我希望這樣的事情:

if {module_pageaddress} contains "/Country/AU/" 
$('#MyDiv').load('state-loader.html .state-AU'); 

if {module_pageaddress} contains "/Country/CA/" 
$('#MyDiv').load('state-loader.html .state-CA'); 

請幫助許多感謝。

回答

0

您可以嘗試

var countryCode = ... // parse the country code from your module  
$('#yourDiv').load('state-loader.html .state-' + countryCode); 

見.load()here更多的例子。

0

至於拉你能做的URL路徑以下

var path_raw = document.location.path, 
    path_array = path_raw.split("/"); 

然後,你可以做這樣的事情:

$.ajax({ 
    url: "./remote_data.php?country=" + path_array[0] + "&state=" + path_array[1], 
    type: "GET", 
    dataType: "JSON", 
    cache: false, 
    success: function(data){ 
      // update all your elements on the page with the data you just grabbed 
    } 
}); 
1

下面是一些代碼:

<!DOCTYPE html> 
<html> 
<head> 
    <title>jQuery test page</title> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js" type="text/javascript"></script> 
    <script type="text/javascript"> 
     function loadContent(elementSelector, sourceURL) { 
      $(""+elementSelector+"").load(""+sourceURL+""); 
     } 
     function stateURL() { 
      var startOfResult = '../../state-loader.html #state-'; 
      var match = (/(?:\/Country\/)(AU|US|CA|UK)(?:\/)/).exec(window.location.pathname); 

      if (match) { 
       return startOfResult + match[1]; 
      } else { 
       return startOfResult + 'AU'; 
      } 
     } 
    </script> 
</head> 
<body> 
<a href="javascript:loadContent('#content', stateURL())">Link 1</a> 
<div id="content">content will be loaded here</div> 
</body> 
</html> 

併爲該文件加載狀態不同的內容:

<!DOCTYPE html> 
<html> 
<head> 
</head> 

<body> 
<div id="state-US">Go USA!</div> 
<div id="state-CA">Go Canada!</div> 
<div id="state-AU">Go Australia!</div> 
<div id="state-UK">Go United Kingdom!</div> 
</body> 
</html> 

看到它在這裏工作:

http://www.quirkscode.com/flat/forumPosts/loadElementContents/Country/US/loadElementContents.html

替換.../US/...與.../AU/...等,看看它是如何表現。

原帖在那裏我得到的想法/原代碼:

http://frinity.blogspot.com/2008/06/load-remote-content-into-div-element.html

0

用我的一條線用於獲取URL段的數組javascript函數:http://joshkoberstein.com/blog/2012/09/get-url-segments-with-javascript

然後,定義變量$ countrySegment是國家代碼所在的分段號碼。

例如: /SEGMENT1 /分段2/CA/

(國家代碼將是段3

然後,檢查第三數組索引被設置,並且如果所述索引是'CA'或'AU'。如果是,則與負載,用在國家代碼段到的.html文件名

function getSegments(){ 
    return location.pathname.split('/').filter(function(e){return e}); 
} 

//set what segment the country code is in 
$countrySegment = 3; 

//get the segments 
$segments = getSegments(); 

//check if segment is set 
//and if segment is either 'AU' or 'CA' 
if(typeof $segments[$countrySegment-1] !==undefined && ($segments[$countrySegment-1] == 'AU' || $segments[$countrySegment-1] == 'CA')){ 
    $countryCode = $segments[$countrySegment-1]; 
    $('#target').load('state-loader.html .state-' + $countryCode); 
} 
0
var result= window.location.pathname.match(/\/Country\/([A-Z]+)\//); 
if(result){ 
    $('#MyDiv').load('state-loader.html .state-' + result[1]); 
} 
相關問題