【问题标题】:Jquery load remote page element according to a string in current page urljquery根据当前页面url中的字符串加载远程页面元素
【发布时间】:2012-09-17 13:53:44
【问题描述】:

我是 Jquery 新手,我想要 Jquery 代码来获取当前页面 url,如果 url 包含特定字符串,则加载远程元素。

示例:

我有这样的页面网址:

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

“/Country/AU”部分是我需要确定我应该加载哪个页面元素,然后如果“AU”我从“/state-loader.html .state-AU”加载,如果“CA”我从“/state-loader.html .state-CA”加载

我有一个内置模块“{module_pageaddress}”来获取当前页面 url 的值,我只是不知道让它工作的 Jquery 逻辑。

我希望是这样的:

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');

请帮忙,非常感谢。

【问题讨论】:

    标签: javascript jquery load


    【解决方案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

    【讨论】:

      【解决方案2】:

      你可以试试

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

      查看 .load() here 的更多示例。

      【讨论】:

        【解决方案3】:

        就拉取 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
             }
        });
        

        【讨论】:

          【解决方案4】:

          使用我的单行 javascript 函数获取 URL 段数组:http://joshkoberstein.com/blog/2012/09/get-url-segments-with-javascript

          然后,将变量 $countrySegment 定义为国家代码所在的航段号。

          例如: /segment1/segment2/CA/

          (国家/地区代码将是 segment 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);
          }
          

          【讨论】:

            【解决方案5】:
            var result= window.location.pathname.match(/\/Country\/([A-Z]+)\//);
            if(result){
                $('#MyDiv').load('state-loader.html .state-' + result[1]);
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2012-08-14
              • 1970-01-01
              • 2012-06-22
              • 1970-01-01
              • 2018-07-01
              • 1970-01-01
              • 2012-11-12
              • 1970-01-01
              相关资源
              最近更新 更多