最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

http redirect - Javascript window.location.href - Refreshes page instead of redirecting - Stack Overflow

programmeradmin0浏览0评论

I'm using window.location.href to redirect my browser and I am not sure why one works and one doesn't. When I use a relative link, it refreshes the current page. When I use the full url it redirects. The page I'm on and the Process.aspx page are on the same directory level. So I should just be able to have a relative link? When I do that though it just reloads the current page I'm on. What basic idea am I missing about window.location.href?

    $(document).ready(function() {

    $( "button" )
        .button();
    $("#cancel")
        .click(function( event ) {
            alert("click");

            //Below Line Doesn't work
            window.location.href = "/Process.aspx";

            //Below Line Does work
            window.location.href = "http://localhost:65215/Process.aspx";
    });
});

I'm using window.location.href to redirect my browser and I am not sure why one works and one doesn't. When I use a relative link, it refreshes the current page. When I use the full url it redirects. The page I'm on and the Process.aspx page are on the same directory level. So I should just be able to have a relative link? When I do that though it just reloads the current page I'm on. What basic idea am I missing about window.location.href?

    $(document).ready(function() {

    $( "button" )
        .button();
    $("#cancel")
        .click(function( event ) {
            alert("click");

            //Below Line Doesn't work
            window.location.href = "/Process.aspx";

            //Below Line Does work
            window.location.href = "http://localhost:65215/Process.aspx";
    });
});
Share Improve this question edited Jul 19, 2013 at 15:57 Pat Lillis 1,1808 silver badges19 bronze badges asked Jul 19, 2013 at 15:47 gbamgbam 1,4844 gold badges19 silver badges30 bronze badges 8
  • 3 What does "doesn't work" mean? What happens? – SLaks Commented Jul 19, 2013 at 15:48
  • 2 Prefer explanation of expected output VS actual output to "doesn't work" – No Results Found Commented Jul 19, 2013 at 15:49
  • 1 Without being able to test... remove the /..? – Stefan Surkamp Commented Jul 19, 2013 at 15:49
  • Sorry, I'll edit and clarify. :( @Stefan, the / doesn't change it. – gbam Commented Jul 19, 2013 at 15:49
  • 1 This might help: stackoverflow.com/questions/503093/… or this stackoverflow.com/questions/1655065/… – Zim84 Commented Jul 19, 2013 at 15:51
 |  Show 3 more comments

5 Answers 5

Reset to default 7

From the Mozilla Developer Network documentation, href is the entire URL of the page. The only relative property in that list is path, which is relative to the host or the domain of the page.

You may also want to look at using the reload or replace method. See How to redirect to another webpage in JavaScript/jQuery?

Somewhat of an edge case, but this problem can also occur if you add event listeners to container divs with anchor tags in them and you want to make each container div, rather than just the anchor tag, clickable and then redirect to the link contained within the anchor tag's href attribute. In this case, you want to include event.preventDefault() to prevent this behavior.

Vanilla JS

document.getElementById("myAnchorDiv").addEventListener("click", function(event){
   event.preventDefault();

    //get url 
    URL = ...

   //Below Line should now work
   window.location.href = URL;
});

jQuery

$("#myAnchorDiv").on("click", function( event ) {
    event.preventDefault();

    //get url 
    URL = ...

    //Below Line should now work
    window.location.href = URL;

 });

If you want, you can of course combine the last two lines of code into just one line of code, i.e. something like window.location.href = [your URL];

Try: location.href = location.origin + "/Process.aspx";

you can try a hack that only refresh the page, you can implement onclick="location.href='wherever'" and a second href="/Process.aspx" onclick will be fired first and href will take you back.

Try this:

window.location = window.location.protocol + 
                  '//' +
                  window.location.hostname +
                  window.location.pathname;
发布评论

评论列表(0)

  1. 暂无评论