Redirecting to a relative URL in JavaScript

前端 未结 7 1962
暗喜
暗喜 2020-11-29 15:40

I have a problem: I want to redirect via JavaScript to a directory above. My code:

location.href = (location.href).substr(0, (location.href).lastIndexOf(\'fo         


        
相关标签:
7条回答
  • 2020-11-29 16:04

    https://developer.mozilla.org/en-US/docs/Web/API/Location/assign

    • window.location.assign("../"); // one level up
    • window.location.assign("/path"); // relative to domain
    0 讨论(0)
  • 2020-11-29 16:05

    try following js code

    location = '..'

    0 讨论(0)
  • 2020-11-29 16:09

    You can do a relative redirect:

    window.location.href = '../'; //one level up
    

    or

    window.location.href = '/path'; //relative to domain
    
    0 讨论(0)
  • 2020-11-29 16:11

    If you use location.hostname you will get your domain.com part. Then location.pathname will give you /path/folder. I would split location.pathname by / and reassemble the URL. But unless you need the querystring, you can just redirect to .. to go a directory above.

    0 讨论(0)
  • 2020-11-29 16:14

    I'm trying to redirect my current web site to other section on the same page, using JavaScript. This follow code work for me:

    location.href='/otherSection'
    
    0 讨论(0)
  • 2020-11-29 16:19

    redirect to ../

    0 讨论(0)
提交回复
热议问题