Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/426.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery/JS:获取当前URL父目录_Javascript_Jquery_Url - Fatal编程技术网

Javascript jQuery/JS:获取当前URL父目录

Javascript jQuery/JS:获取当前URL父目录,javascript,jquery,url,Javascript,Jquery,Url,发件人: 我怎样才能得到: 并使用Javascript将其存储到变量中,以及如何使用jQuery。提前感谢。 var a = "http://www.site.com/example/index.html"; var b = a.substring(0, a.lastIndexOf('/'))+"/"; 正则表达式也可以这样做,但在本例中,正则表达式并不是最简单的解决方案 var url = "http://www.site.com/example/index.html"; var new

发件人:

我怎样才能得到:

并使用Javascript将其存储到变量中,以及如何使用jQuery。提前感谢。

var a = "http://www.site.com/example/index.html";
var b = a.substring(0, a.lastIndexOf('/'))+"/";

正则表达式也可以这样做,但在本例中,正则表达式并不是最简单的解决方案

var url = "http://www.site.com/example/index.html";
var newUrl = url.match(/^(.*[\\\/])/)[1];


当前页面的演示过程:

  • $(地点)

  • $(位置).prop(“href”)

  • $(位置).prop(“href”).split(“/”)

  • $(位置).prop(“href”).split(“/”).slice(0,-1)

    ※slice()方法选择从给定开始参数开始的元素,并在给定结束参数结束,但不包括。使用负数从数组末尾进行选择

  • $(location).prop(“href”).split(“/”).slice(0,-1).join(“/”)


  • 注释和参考资料:

    • :location对象包含有关当前URL的信息
    • :当前页面的整个URL
    • :获取元素的属性值
    • :该方法用于将字符串拆分为子字符串数组,并返回新数组
    • :该方法将数组中的选定元素作为新数组对象返回
    • :该方法将数组的元素合并为字符串,并返回字符串

      • 以下方法似乎有效

        新URL(“.”,“http://example.com/folder/subfolder/file.js")
        
        小挑剔。。。那不是“目录”。它是资源URL的一部分。它可能映射到服务器上的一个目录,但URL本身没有目录,因为HTTP不是文件系统。感谢您提供的信息,非常感谢您是否介意扩展您的答案,以供其他程序员理解,它到底如何帮助解决问题。这是一个更好的答案,因为您可以很容易地找到祖父母,等
        var url = "http://www.site.com/example/index.html";
        var newUrl = url.match(/^(.*[\\\/])/)[1];
        
        var s1 = "http://www.site.com/example/index.html";
        var s2 = s1.replace(s1.split("/").pop(),"");
        
        $(location).prop("href").split("/").slice(0,-1).join("/")
        
        {
            "ancestorOrigins": {
            },
            "hash": "",
            "host": "stackoverflow.com",
            "hostname": "stackoverflow.com",
            "href": "https://stackoverflow.com/questions/17497045/jquery-js-get-current-url-parent-directory",
            "origin": "https://stackoverflow.com",
            "pathname": "/questions/17497045/jquery-js-get-current-url-parent-directory",
            "port": "",
            "protocol": "https:",
            "search": ""
        }
        
        https://stackoverflow.com/questions/17497045/jquery-js-get-current-url-parent-directory
        
        [
            "https:",
            "",
            "stackoverflow.com",
            "questions",
            "17497045",
            "jquery-js-get-current-url-parent-directory"
        ]
        
        [
            "https:",
            "",
            "stackoverflow.com",
            "questions",
            "17497045"
        ]
        
        https://stackoverflow.com/questions/17497045