如何使用javascript提取然后更改url路径? [英] How can I extract and then change the url path using javascript?

查看:91
本文介绍了如何使用javascript提取然后更改url路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试提取部分 url 并使用 javascript 将其替换为自定义文本.

I am trying to extract part of the url and replace it with custom text using javascript.

比如我想获取当前的url如:
mydomain.com/url_part_to_change/some-other-stuff

For example, I want to fetch the current url such as:
mydomain.com/url_part_to_change/some-other-stuff

然后将该网址更改为插入,以便新的新网址为:
mydomain.com/new_url_part/some-other-stuff

and then change that url to insert so that new new url is:
mydomain.com/new_url_part/some-other-stuff

这是我所拥有的:

function changeURL() {
        var theURL = window.location.pathname;
        theURL.replace("/url_part_to_change/", "/new_url_part/");
        //Set URL
}

但是,当我尝试调用函数 changeURL() 时,它返回 undefined 而不是新的 url.

However, when I try to call the function changeURL(), it returns undefined instead of the new url.

例如,如果我这样做:

alert(changeURL());

那么什么警报是undefined

推荐答案

你没有在函数中返回任何东西,请让函数像

you are not returning any thing in function, Please make function like

function changeURL() {
        var theURL = window.location.pathname;
       return  theURL.replace("/url_part_to_change/", "/new_url_part/");
        //Set URL

}

这篇关于如何使用javascript提取然后更改url路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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