如何动态地将一个URL添加到javascript中,以获得第二页的div在第一页上显示? [英] How can I dynamically add a URL into javascript, to get a second page's div to display on the first page?

查看:1026
本文介绍了如何动态地将一个URL添加到javascript中,以获得第二页的div在第一页上显示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我有这样的工作,起始代码:

  $(document).ready (){
$('.currentpagediv').load('http://theurl.com/page2/somethingsomewhere.html .secondpagedivclass');
});

这是什么,找到当前页面上的div, div class =currentpagediv> ,并添加到第二页( http://theurl.com/page2/somethingsomewhere.html ) div < div class =secondpagedivclass>



所以例如说我有一个这样的页面:

 < html> 
< body>
< div class =currentpagediv>
< / div>
< / body>
< / html>

然后我上面的代码是这样做的:

 < html> 
< body>
< div class =currentpagediv>
< / div>
< div class =secondpagedivclass>
< / div>
< / body>
< / html>

这是我想要的。所以我有我需要的功能。



但是,问题是我需要URL部分是 动态

例如,当前页面总是 http://theurl.com/page1/ [一个路径] .html 和新的我需要获取div的页面始终为 http://theurl.com/page2/ [同一路径] .html



所以基本上,我需要得到这个URL,并且只将 / page1 / 更改为 / page2 / ,同时保留这个。这样我可以在域的所有页面上运行,它将从 page2 添加到 page1 中的部分。 / p>

像这样:






原始页 http://theurl.com/page1/365743668.html

  ; HTML> 
< body>
< div class =currentpagediv>
< / div>
< / body>
< / html>

第二页 http://theurl.com/page2/365743668.html

 < html> 
< body>
< div class =secondpagedivclass>
< / div>
< / body>
< / html>

正文正在运行的新的原始页面(仍然 http ://theurl.com/page1/365743668.html ):

  ; HTML> 
< body>
< div class =currentpagediv>
< / div>
< div class =secondpagedivclass>
[信息从页面http:// theurl.com / page2 / 365743668.html]
< / div>
< / body>
< / html>

我尝试过很多东西,但没有工作。



这是我的尝试不起作用:






  function changeURL(){
var theURL = location.pathname.toString();
var newURL = theURL.replace(/ page1 /,/ page2 /);
}
$(document).ready(function(){
$('。currentpagediv')load(changeURL()'.secondpagedivclass');
});

在上面的代码中,我尝试:




  • 获取当前页面的URL

  • 将URL转换为字符串,因此我可以

  • 修改url,然后

  • 添加函数 changeURL()代替URL在<。c $ c> .load之后('






  • 请注意我想要使用 jquery 此方法工作(不是其他方法),因为我也在尝试学习,给我一些完全不同的东西不会帮助我学习如何做这个方法。

    解决方案

    你只是错过了字符串连接操作, +

      $('。currentpagediv')。load(changeURL()+'.secondpagedivclass'); 

    不要忘记 .secondpagedivclass 之前的空格。 / p>

    changeURL 函数需要返回结果:

      function changeURL(){
    var theURL = location.pathname;
    var newURL = theURL.replace(/ page1 /,/ page2 /);
    return newURL;
    }

    您不需要使用 .toString( ),因为路径名是一个字符串。


    Ok, so this what I have that works, for the starting code:

    $(document).ready(function(){
      $('.currentpagediv').load('http://theurl.com/page2/somethingsomewhere.html .secondpagedivclass');
    });
    

    What this does, is find the div on the current page, <div class="currentpagediv">, and add in the second page (http://theurl.com/page2/somethingsomewhere.html) div <div class="secondpagedivclass">

    So for example, say I have a page like this:

    <html>
      <body>
        <div class="currentpagediv">
        </div>
      </body>
    </html>
    

    then what my code above does is make this:

    <html>
      <body>
        <div class="currentpagediv">
        </div>
        <div class="secondpagedivclass">
        </div>
      </body>
    </html>
    

    Which is just what I want it to do. So I have the functionality I need.

    BUT, the problem is that I need the URL portion to be dynamic.
    For example, the current page is always http://theurl.com/page1/[a path].html, and the new page that I need to fetch the div is always http://theurl.com/page2/[the same path].html

    So basically, I need to get the URL, and change ONLY /page1/ to /page2/, while retaining this. That way I can run on all the pages of the domain and it will add the section from page2 into page1.

    Like this:


    Original page http://theurl.com/page1/365743668.html:

    <html>
      <body>
        <div class="currentpagediv">
        </div>
      </body>
    </html>
    

    Second page http://theurl.com/page2/365743668.html:

    <html>
      <body>
        <div class="secondpagedivclass">
        </div>
      </body>
    </html>
    

    NEW ORIGINAL PAGE THAT THE SCRIPT IS RUNNING ON (still http://theurl.com/page1/365743668.html):

    <html>
      <body>
        <div class="currentpagediv">
        </div>
        <div class="secondpagedivclass">
           [THE INFO FROM PAGE `http://theurl.com/page2/365743668.html`]
        </div>
      </body>
    </html>
    

    I've tried many things, but they did not work.

    Here is my attempt which does not work:


    function changeURL() {
            var theURL = location.pathname.toString();
            var newURL = theURL.replace("/page1/", "/page2/");
    }
    $(document).ready(function(){
      $('.currentpagediv').load(changeURL() '.secondpagedivclass');
    });
    

    In the code above, I tried to:

    1. fetch the url of current page
    2. convert the url to a string, so I could
    3. modify the url, and then
    4. add the function changeURL() in place of where the URL went after .load('


    Please note I want to make it work using jquery and this method (NOT ANOTHER METHOD) because I'm also trying to learn, and giving me something completely different is not going to help me learn how to do this method.

    解决方案

    You're just missing the string concatenation operation, +.

    $('.currentpagediv').load(changeURL() + ' .secondpagedivclass');
    

    Don't forget the space before .secondpagedivclass.

    The changeURL function needs to return the result:

    function changeURL() {
        var theURL = location.pathname;
        var newURL = theURL.replace("/page1/", "/page2/");
        return newURL;
    }
    

    You don't need to use .toString(), since the pathname is a string.

    这篇关于如何动态地将一个URL添加到javascript中,以获得第二页的div在第一页上显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆