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

后端 未结 9 1443
借酒劲吻你
借酒劲吻你 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:48

    You can use Javascript URLSearchParams.

    var url = new URL(window.location.href);
    url.searchParams.set('single','');
    window.location.href = url.href;
    

    [UPDATE]: If IE support is a need, check this thread:

    SCRIPT5009: 'URLSearchParams' is undefined in IE 11

    Thanks @john-m to talk about the IE support

    0 讨论(0)
  • 2020-12-13 17:52

    Concision counts: I prefer window.location = "?single"; or window.location += "?single";

    0 讨论(0)
  • 2020-12-13 17:52

    Click these links to see these more flexible and robust solutions. They're answers to a similar question:

    • With jQuery and the query plug-in: window.location.search = jQuery.query.set('single', true);
    • Without jQuery: Use parse and stringify on window.location.search

    These allow you to programmatically set the parameter, and, unlike the other hacks suggested for this question, won't break for URLs that already have a parameter, or if something else isn't quite what you thought might happen.

    0 讨论(0)
  • 2020-12-13 17:53

    You should be able to accomplish this by using location.href

    if(window.location.hostname == "www.myweb.com"){
       window.location.href = window.location.href + "?single";
    }
    
    0 讨论(0)
  • 2020-12-13 17:53

    I'm using Jquery Load to handels this, works great for me. check out my code from my project. I need to refresh with arguments to put Javascript variable into php

    if (isset($_GET['language'])){
        $language = $_GET['language'];
    }else{
        echo '<script>';    
        echo '  var userLang = navigator.language || navigator.userLanguage;';
        echo '  if(userLang.search("zh") != -1) {';
        echo '      var language = "chn";';
        echo '  }else{';
        echo '      var language = "eng";';
        echo '  }';
        echo '$("html").load("index.php","language=" + language);';
        echo '</script>';
        die;
    }
    
    0 讨论(0)
  • 2020-12-13 17:54

    if window.location.hash is empty, you cant assign to location.href a new value without using a correct function (at least tested in chrome).

    try the window.location.replace:

    if (!window.location.hash) 
        {
            window.location.replace(window.location.href + "?single")
        } 
    
    0 讨论(0)
提交回复
热议问题