Redirecting to a relative URL in JavaScript












288















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('folder'));


The url looks like this:




example.com/path/folder/index.php?file=abc&test=123&lol=cool




The redirect affect just this:




example.com/path/&test=123&lol=cool




But want to have this:




example.com/path/




How could I do that?










share|improve this question





























    288















    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('folder'));


    The url looks like this:




    example.com/path/folder/index.php?file=abc&test=123&lol=cool




    The redirect affect just this:




    example.com/path/&test=123&lol=cool




    But want to have this:




    example.com/path/




    How could I do that?










    share|improve this question



























      288












      288








      288


      39






      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('folder'));


      The url looks like this:




      example.com/path/folder/index.php?file=abc&test=123&lol=cool




      The redirect affect just this:




      example.com/path/&test=123&lol=cool




      But want to have this:




      example.com/path/




      How could I do that?










      share|improve this question
















      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('folder'));


      The url looks like this:




      example.com/path/folder/index.php?file=abc&test=123&lol=cool




      The redirect affect just this:




      example.com/path/&test=123&lol=cool




      But want to have this:




      example.com/path/




      How could I do that?







      javascript url redirect






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Aug 20 '18 at 20:35









      AnkDasCo

      595314




      595314










      asked Oct 31 '09 at 17:43









      user199337user199337

      3,06371917




      3,06371917
























          6 Answers
          6






          active

          oldest

          votes


















          548














          You can do a relative redirect:



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


          or



          window.location.href = '/path'; //relative to domain





          share|improve this answer





















          • 22





            see why you should use window.location.replace stackoverflow.com/questions/503093/…

            – gideon
            Dec 14 '10 at 5:32






          • 42





            When you want to simulate a link, you should use window.location.href. You should only use window.location.replace when you want to simulate an http redirect (thus not generating a history item).

            – Benji XVI
            Oct 13 '11 at 14:50






          • 21





            By the way, document.location was intended as a read-only property. It is safer to use window.location. See this question.

            – Benji XVI
            Oct 13 '11 at 14:53








          • 6





            I found using window.location.href = '../' redirected to the root of the site and not "one level up" as expected. When the current page is "www.example.com/customers/list" I needed to use './'. I guess this is because "list" is not considered as a directory level.

            – Marcus Cunningham
            Nov 8 '16 at 10:24






          • 2





            @MarcusCunningham, in your example, the directory is /customers/ - so "one level up" is www.example.com/. Now if your example URL was www.example.com/customers/list/ - it would redirect you to www.example.com/customers/

            – Ubeogesh
            Jun 4 '18 at 11:54



















          12














          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.






          share|improve this answer


























          • My browser barks when I go location.path and it seems only to recognize location.pathname. Hints?

            – Konrad Viltersten
            Jan 9 '16 at 20:19











          • location.path is incorrect, it should be location.hostname. I've added an edit to the post to fix this.

            – ygrichman
            Jan 6 '17 at 7:07



















          8














          redirect to ../






          share|improve this answer



















          • 2





            Why the DV? Does this ever not work?

            – Chris Ballance
            Oct 31 '09 at 17:54











          • If your app is hosted in a sub-uri and the app doesn't know the sub-uri path. If you are at your apps root and you do ../ the app won't know how to get back to its root. For example, the same app is hosted at example.com/myapp and other.example.com/app2

            – ReggieB
            Jan 16 '14 at 9:29



















          6














          <a href="..">no JS needed</a>



          .. means parent directory.






          share|improve this answer



















          • 11





            That requires a click or some other user-initiated navigation.

            – recursive
            Sep 28 '12 at 22:44











          • Thanks for this. This saved time for me

            – karthickj25
            Feb 18 '18 at 8:46



















          2














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





          • window.location.assign("../"); // one level up


          • window.location.assign("/path"); // relative to domain






          share|improve this answer































            1














            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'





            share|improve this answer
























              protected by Community Sep 23 '17 at 7:24



              Thank you for your interest in this question.
              Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



              Would you like to answer one of these unanswered questions instead?














              6 Answers
              6






              active

              oldest

              votes








              6 Answers
              6






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              548














              You can do a relative redirect:



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


              or



              window.location.href = '/path'; //relative to domain





              share|improve this answer





















              • 22





                see why you should use window.location.replace stackoverflow.com/questions/503093/…

                – gideon
                Dec 14 '10 at 5:32






              • 42





                When you want to simulate a link, you should use window.location.href. You should only use window.location.replace when you want to simulate an http redirect (thus not generating a history item).

                – Benji XVI
                Oct 13 '11 at 14:50






              • 21





                By the way, document.location was intended as a read-only property. It is safer to use window.location. See this question.

                – Benji XVI
                Oct 13 '11 at 14:53








              • 6





                I found using window.location.href = '../' redirected to the root of the site and not "one level up" as expected. When the current page is "www.example.com/customers/list" I needed to use './'. I guess this is because "list" is not considered as a directory level.

                – Marcus Cunningham
                Nov 8 '16 at 10:24






              • 2





                @MarcusCunningham, in your example, the directory is /customers/ - so "one level up" is www.example.com/. Now if your example URL was www.example.com/customers/list/ - it would redirect you to www.example.com/customers/

                – Ubeogesh
                Jun 4 '18 at 11:54
















              548














              You can do a relative redirect:



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


              or



              window.location.href = '/path'; //relative to domain





              share|improve this answer





















              • 22





                see why you should use window.location.replace stackoverflow.com/questions/503093/…

                – gideon
                Dec 14 '10 at 5:32






              • 42





                When you want to simulate a link, you should use window.location.href. You should only use window.location.replace when you want to simulate an http redirect (thus not generating a history item).

                – Benji XVI
                Oct 13 '11 at 14:50






              • 21





                By the way, document.location was intended as a read-only property. It is safer to use window.location. See this question.

                – Benji XVI
                Oct 13 '11 at 14:53








              • 6





                I found using window.location.href = '../' redirected to the root of the site and not "one level up" as expected. When the current page is "www.example.com/customers/list" I needed to use './'. I guess this is because "list" is not considered as a directory level.

                – Marcus Cunningham
                Nov 8 '16 at 10:24






              • 2





                @MarcusCunningham, in your example, the directory is /customers/ - so "one level up" is www.example.com/. Now if your example URL was www.example.com/customers/list/ - it would redirect you to www.example.com/customers/

                – Ubeogesh
                Jun 4 '18 at 11:54














              548












              548








              548







              You can do a relative redirect:



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


              or



              window.location.href = '/path'; //relative to domain





              share|improve this answer















              You can do a relative redirect:



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


              or



              window.location.href = '/path'; //relative to domain






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 18 '18 at 17:24

























              answered Oct 31 '09 at 17:50









              KobiKobi

              107k34221264




              107k34221264








              • 22





                see why you should use window.location.replace stackoverflow.com/questions/503093/…

                – gideon
                Dec 14 '10 at 5:32






              • 42





                When you want to simulate a link, you should use window.location.href. You should only use window.location.replace when you want to simulate an http redirect (thus not generating a history item).

                – Benji XVI
                Oct 13 '11 at 14:50






              • 21





                By the way, document.location was intended as a read-only property. It is safer to use window.location. See this question.

                – Benji XVI
                Oct 13 '11 at 14:53








              • 6





                I found using window.location.href = '../' redirected to the root of the site and not "one level up" as expected. When the current page is "www.example.com/customers/list" I needed to use './'. I guess this is because "list" is not considered as a directory level.

                – Marcus Cunningham
                Nov 8 '16 at 10:24






              • 2





                @MarcusCunningham, in your example, the directory is /customers/ - so "one level up" is www.example.com/. Now if your example URL was www.example.com/customers/list/ - it would redirect you to www.example.com/customers/

                – Ubeogesh
                Jun 4 '18 at 11:54














              • 22





                see why you should use window.location.replace stackoverflow.com/questions/503093/…

                – gideon
                Dec 14 '10 at 5:32






              • 42





                When you want to simulate a link, you should use window.location.href. You should only use window.location.replace when you want to simulate an http redirect (thus not generating a history item).

                – Benji XVI
                Oct 13 '11 at 14:50






              • 21





                By the way, document.location was intended as a read-only property. It is safer to use window.location. See this question.

                – Benji XVI
                Oct 13 '11 at 14:53








              • 6





                I found using window.location.href = '../' redirected to the root of the site and not "one level up" as expected. When the current page is "www.example.com/customers/list" I needed to use './'. I guess this is because "list" is not considered as a directory level.

                – Marcus Cunningham
                Nov 8 '16 at 10:24






              • 2





                @MarcusCunningham, in your example, the directory is /customers/ - so "one level up" is www.example.com/. Now if your example URL was www.example.com/customers/list/ - it would redirect you to www.example.com/customers/

                – Ubeogesh
                Jun 4 '18 at 11:54








              22




              22





              see why you should use window.location.replace stackoverflow.com/questions/503093/…

              – gideon
              Dec 14 '10 at 5:32





              see why you should use window.location.replace stackoverflow.com/questions/503093/…

              – gideon
              Dec 14 '10 at 5:32




              42




              42





              When you want to simulate a link, you should use window.location.href. You should only use window.location.replace when you want to simulate an http redirect (thus not generating a history item).

              – Benji XVI
              Oct 13 '11 at 14:50





              When you want to simulate a link, you should use window.location.href. You should only use window.location.replace when you want to simulate an http redirect (thus not generating a history item).

              – Benji XVI
              Oct 13 '11 at 14:50




              21




              21





              By the way, document.location was intended as a read-only property. It is safer to use window.location. See this question.

              – Benji XVI
              Oct 13 '11 at 14:53







              By the way, document.location was intended as a read-only property. It is safer to use window.location. See this question.

              – Benji XVI
              Oct 13 '11 at 14:53






              6




              6





              I found using window.location.href = '../' redirected to the root of the site and not "one level up" as expected. When the current page is "www.example.com/customers/list" I needed to use './'. I guess this is because "list" is not considered as a directory level.

              – Marcus Cunningham
              Nov 8 '16 at 10:24





              I found using window.location.href = '../' redirected to the root of the site and not "one level up" as expected. When the current page is "www.example.com/customers/list" I needed to use './'. I guess this is because "list" is not considered as a directory level.

              – Marcus Cunningham
              Nov 8 '16 at 10:24




              2




              2





              @MarcusCunningham, in your example, the directory is /customers/ - so "one level up" is www.example.com/. Now if your example URL was www.example.com/customers/list/ - it would redirect you to www.example.com/customers/

              – Ubeogesh
              Jun 4 '18 at 11:54





              @MarcusCunningham, in your example, the directory is /customers/ - so "one level up" is www.example.com/. Now if your example URL was www.example.com/customers/list/ - it would redirect you to www.example.com/customers/

              – Ubeogesh
              Jun 4 '18 at 11:54













              12














              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.






              share|improve this answer


























              • My browser barks when I go location.path and it seems only to recognize location.pathname. Hints?

                – Konrad Viltersten
                Jan 9 '16 at 20:19











              • location.path is incorrect, it should be location.hostname. I've added an edit to the post to fix this.

                – ygrichman
                Jan 6 '17 at 7:07
















              12














              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.






              share|improve this answer


























              • My browser barks when I go location.path and it seems only to recognize location.pathname. Hints?

                – Konrad Viltersten
                Jan 9 '16 at 20:19











              • location.path is incorrect, it should be location.hostname. I've added an edit to the post to fix this.

                – ygrichman
                Jan 6 '17 at 7:07














              12












              12








              12







              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.






              share|improve this answer















              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.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Jan 6 '17 at 7:06









              ygrichman

              6,047167




              6,047167










              answered Oct 31 '09 at 17:49









              BobBob

              68.1k27107122




              68.1k27107122













              • My browser barks when I go location.path and it seems only to recognize location.pathname. Hints?

                – Konrad Viltersten
                Jan 9 '16 at 20:19











              • location.path is incorrect, it should be location.hostname. I've added an edit to the post to fix this.

                – ygrichman
                Jan 6 '17 at 7:07



















              • My browser barks when I go location.path and it seems only to recognize location.pathname. Hints?

                – Konrad Viltersten
                Jan 9 '16 at 20:19











              • location.path is incorrect, it should be location.hostname. I've added an edit to the post to fix this.

                – ygrichman
                Jan 6 '17 at 7:07

















              My browser barks when I go location.path and it seems only to recognize location.pathname. Hints?

              – Konrad Viltersten
              Jan 9 '16 at 20:19





              My browser barks when I go location.path and it seems only to recognize location.pathname. Hints?

              – Konrad Viltersten
              Jan 9 '16 at 20:19













              location.path is incorrect, it should be location.hostname. I've added an edit to the post to fix this.

              – ygrichman
              Jan 6 '17 at 7:07





              location.path is incorrect, it should be location.hostname. I've added an edit to the post to fix this.

              – ygrichman
              Jan 6 '17 at 7:07











              8














              redirect to ../






              share|improve this answer



















              • 2





                Why the DV? Does this ever not work?

                – Chris Ballance
                Oct 31 '09 at 17:54











              • If your app is hosted in a sub-uri and the app doesn't know the sub-uri path. If you are at your apps root and you do ../ the app won't know how to get back to its root. For example, the same app is hosted at example.com/myapp and other.example.com/app2

                – ReggieB
                Jan 16 '14 at 9:29
















              8














              redirect to ../






              share|improve this answer



















              • 2





                Why the DV? Does this ever not work?

                – Chris Ballance
                Oct 31 '09 at 17:54











              • If your app is hosted in a sub-uri and the app doesn't know the sub-uri path. If you are at your apps root and you do ../ the app won't know how to get back to its root. For example, the same app is hosted at example.com/myapp and other.example.com/app2

                – ReggieB
                Jan 16 '14 at 9:29














              8












              8








              8







              redirect to ../






              share|improve this answer













              redirect to ../







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Oct 31 '09 at 17:48









              Chris BallanceChris Ballance

              23.4k2594144




              23.4k2594144








              • 2





                Why the DV? Does this ever not work?

                – Chris Ballance
                Oct 31 '09 at 17:54











              • If your app is hosted in a sub-uri and the app doesn't know the sub-uri path. If you are at your apps root and you do ../ the app won't know how to get back to its root. For example, the same app is hosted at example.com/myapp and other.example.com/app2

                – ReggieB
                Jan 16 '14 at 9:29














              • 2





                Why the DV? Does this ever not work?

                – Chris Ballance
                Oct 31 '09 at 17:54











              • If your app is hosted in a sub-uri and the app doesn't know the sub-uri path. If you are at your apps root and you do ../ the app won't know how to get back to its root. For example, the same app is hosted at example.com/myapp and other.example.com/app2

                – ReggieB
                Jan 16 '14 at 9:29








              2




              2





              Why the DV? Does this ever not work?

              – Chris Ballance
              Oct 31 '09 at 17:54





              Why the DV? Does this ever not work?

              – Chris Ballance
              Oct 31 '09 at 17:54













              If your app is hosted in a sub-uri and the app doesn't know the sub-uri path. If you are at your apps root and you do ../ the app won't know how to get back to its root. For example, the same app is hosted at example.com/myapp and other.example.com/app2

              – ReggieB
              Jan 16 '14 at 9:29





              If your app is hosted in a sub-uri and the app doesn't know the sub-uri path. If you are at your apps root and you do ../ the app won't know how to get back to its root. For example, the same app is hosted at example.com/myapp and other.example.com/app2

              – ReggieB
              Jan 16 '14 at 9:29











              6














              <a href="..">no JS needed</a>



              .. means parent directory.






              share|improve this answer



















              • 11





                That requires a click or some other user-initiated navigation.

                – recursive
                Sep 28 '12 at 22:44











              • Thanks for this. This saved time for me

                – karthickj25
                Feb 18 '18 at 8:46
















              6














              <a href="..">no JS needed</a>



              .. means parent directory.






              share|improve this answer



















              • 11





                That requires a click or some other user-initiated navigation.

                – recursive
                Sep 28 '12 at 22:44











              • Thanks for this. This saved time for me

                – karthickj25
                Feb 18 '18 at 8:46














              6












              6








              6







              <a href="..">no JS needed</a>



              .. means parent directory.






              share|improve this answer













              <a href="..">no JS needed</a>



              .. means parent directory.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Oct 31 '09 at 17:49









              KornelKornel

              76k28171233




              76k28171233








              • 11





                That requires a click or some other user-initiated navigation.

                – recursive
                Sep 28 '12 at 22:44











              • Thanks for this. This saved time for me

                – karthickj25
                Feb 18 '18 at 8:46














              • 11





                That requires a click or some other user-initiated navigation.

                – recursive
                Sep 28 '12 at 22:44











              • Thanks for this. This saved time for me

                – karthickj25
                Feb 18 '18 at 8:46








              11




              11





              That requires a click or some other user-initiated navigation.

              – recursive
              Sep 28 '12 at 22:44





              That requires a click or some other user-initiated navigation.

              – recursive
              Sep 28 '12 at 22:44













              Thanks for this. This saved time for me

              – karthickj25
              Feb 18 '18 at 8:46





              Thanks for this. This saved time for me

              – karthickj25
              Feb 18 '18 at 8:46











              2














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





              • window.location.assign("../"); // one level up


              • window.location.assign("/path"); // relative to domain






              share|improve this answer




























                2














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





                • window.location.assign("../"); // one level up


                • window.location.assign("/path"); // relative to domain






                share|improve this answer


























                  2












                  2








                  2







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





                  • window.location.assign("../"); // one level up


                  • window.location.assign("/path"); // relative to domain






                  share|improve this answer













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





                  • window.location.assign("../"); // one level up


                  • window.location.assign("/path"); // relative to domain







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 16 '17 at 3:31









                  Shaun LuttinShaun Luttin

                  59.2k33227287




                  59.2k33227287























                      1














                      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'





                      share|improve this answer






























                        1














                        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'





                        share|improve this answer




























                          1












                          1








                          1







                          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'





                          share|improve this answer















                          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'






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Sep 22 '17 at 22:07









                          Krinkle

                          8,11642342




                          8,11642342










                          answered Sep 13 '17 at 18:04









                          Jorge Santos NeillJorge Santos Neill

                          1052




                          1052

















                              protected by Community Sep 23 '17 at 7:24



                              Thank you for your interest in this question.
                              Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



                              Would you like to answer one of these unanswered questions instead?



                              這個網誌中的熱門文章

                              Xamarin.form Move up view when keyboard appear

                              Post-Redirect-Get with Spring WebFlux and Thymeleaf

                              Anylogic : not able to use stopDelay()