javascript 如何使用javascript提取然后更改url路径?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30137059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 11:46:39  来源:igfitidea点击:

How can I extract and then change the url path using javascript?

javascripturlpath

提问by

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

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

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

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

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

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

Here is what I have:

这是我所拥有的:

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

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

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



For example if I do this:

例如,如果我这样做:

alert(changeURL());

then what alerts is undefined

那么什么是警报 undefined

采纳答案by abhi

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

}

回答by daxeh

TL;DR

TL; 博士

// update the pathname that will reload the page
window.location.pathname = myNewPathname;


Further Explanation:

进一步说明:

Window.location( image attached below ) provides you an object containing all the uri parts information. So, you can get this object via window.locationand access the property pathnamethen do your stuffs. For example:

Window.location(下附图片)为您提供了一个包含所有 uri 部件信息的对象。因此,您可以通过获取此对象window.location并访问该属性,pathname然后执行您的操作。例如:

var locationObject = window.location;
var pathnameToChange = locationObject.pathname;

// do stuffs to "copy" of pathname, this will not reload the page
var myNewPathname = doSomethingMyPathname( pathnameToChange );

Additional Examples:

其他示例:

Alternatively, set new url using location.href. Check the MDN documentationfor examples on location.assign(), location.replace(), location.reload()and notes on the different available functions

或者,使用location.href. 检查MDN文档上的例子location.assign()location.replace()location.reload()和注意事项在不同的可用功能

// ie.myNewUrl is something I created -> www.blah.com/updated/path
window.location.href = myNewUrl; 

// or
window.location.assign(myNewUrl)

A window.location Object in Console

控制台中的 window.location 对象

window.location

窗口位置

There are three references to further understand URI components

进一步了解URI组件的三个参考

  1. URI_scheme
  2. Standards written by Tim Berners-Lee
  3. MDN Location
  1. URI_scheme
  2. Tim Berners-Lee 编写的标准
  3. MDN 位置

Hope this helps.

希望这可以帮助。

回答by Criss Lion

This should work for you correctly:

这应该适合你:

function changeURL() {
        // Get the url, just as you did
        var theURL = window.location.pathname;
        // Return the url
        return theURL.replace("/url_part_to_change/", "/new_url_part/"); 
}

回答by JanS

As the others said, you don't return anything. What they are forgetting is that String.replace() just makes a copy of theURLand doesn't change theURL.

正如其他人所说,你什么都不返回。他们忘记的是 String.replace() 只是复制theURL并且不会改变theURL

Try this:

试试这个:

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

  return theURL;
}
alert(changeURL());

回答by Zee

You forgot to return

你忘了 return

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

alert(changeURL())//Now you won't see undefined.

回答by Vishal Singh

   function changeURL() {
      //set new path
      window.location.pathname = "/new_url_part/";

      //get new url
      const newURL = window.location.href;
      return newURL;
    }