How to refresh a page with jQuery by passing a parameter to URL

后端 未结 9 1444
借酒劲吻你
借酒劲吻你 2020-12-13 17:04

I am refreshing my page using jQuery:

location.reload();

This is working great but I want to refresh the same page by passing a parameter t

相关标签:
9条回答
  • 2020-12-13 17:54
    var singleText = "single";
    var s = window.location.search;
    
    if (s.indexOf(singleText) == -1) {
        window.location.href += (s.substring(0,1) == "?") ? "&" : "?" + singleText;
    }
    
    0 讨论(0)
  • 2020-12-13 17:58

    I would use REGEX with .replace like this:

    window.location.href = window.location.href.replace( /[\?#].*|$/, "?single" );
    
    0 讨论(0)
  • 2020-12-13 17:58

    You could simply have just done:

    var varAppend = "?single";
    window.location.href = window.location.href.replace(".com",".com" + varAppend);
    

    Unlike the other answers provided, there is no needless conditional check. If you design your project properly, you'll let the interface make the decision making and calling that statement whenever an event has been triggered.

    Since there will only be one ".com" in your url, it will just replace .com with .com?single. I just added varAppend just in case you want to make it easier to modify the code in the future with different kinds of url variables.

    One other note: The .replace works by adding to the href since href returns a string containing the full url address information.

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