如何通过将参数传递给URL来刷新jQuery页面

时间:2021-09-03 10:28:08

I am refreshing my page using jQuery:

我正在用jQuery刷新我的页面:

location.reload();

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

这非常有用,但是我想通过向URL传递一个参数来刷新相同的页面。

How can I do this by using jQuery?

如何使用jQuery实现这一点?

Ex:

例:

If my url is www.myweb.com, I want to refresh this by passing a parameter like this

如果我的url是www.myweb.com,我想通过传递这样的参数来刷新它

  www.myweb.com?single

Thank you

谢谢你!

9 个解决方案

#1


41  

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

您应该能够通过使用location.href来实现这一点

if(window.location.hostname == "www.myweb.com"){
   window.location.href = window.location.href + "?single";
}

#2


40  

I would use REGEX with .replace like this:

我将使用REGEX。

window.location.href = window.location.href.replace( /[\?#].*|$/, "?single" );

#3


3  

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).

如果window.location。哈希是空的,你不能分配到位置。href一个没有使用正确函数的新值(至少在chrome中测试过)。

try the window.location.replace:

尝试window.location.replace:

if (!window.location.hash) 
    {
        window.location.replace(window.location.href + "?single")
    } 

#4


2  

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

我更喜欢窗户。位置= " ?单”;或窗口。位置+ =”?单”;

#5


2  

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.

因为你的url中只有一个“。com”,它会用。我只是添加了varAppend,以防您希望以后用不同类型的url变量修改代码更容易一些。

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

另一个注意事项:.replace工作通过向href添加,因为href返回包含完整url地址信息的字符串。

#6


2  

You can use Javascript URLSearchParams.

您可以使用Javascript URLSearchParams。

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

#7


1  

var singleText = "single";
var s = window.location.search;

if (s.indexOf(singleText) == -1) {
    window.location.href += (s.substring(0,1) == "?") ? "&" : "?" + singleText;
}

#8


1  

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

单击这些链接,可以看到这些更加灵活和健壮的解决方案。他们回答了一个类似的问题:

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.

这些允许您以编程方式设置参数,并且与本问题中建议的其他技巧不同,对于已经具有参数的url,或者如果您认为可能发生的其他事情不太一样,这些技巧不会中断。

#9


0  

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

我用Jquery的Load来处理这个,对我来说很有用。查看我的项目代码。我需要刷新参数,将Javascript变量放入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;
}

#1


41  

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

您应该能够通过使用location.href来实现这一点

if(window.location.hostname == "www.myweb.com"){
   window.location.href = window.location.href + "?single";
}

#2


40  

I would use REGEX with .replace like this:

我将使用REGEX。

window.location.href = window.location.href.replace( /[\?#].*|$/, "?single" );

#3


3  

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).

如果window.location。哈希是空的,你不能分配到位置。href一个没有使用正确函数的新值(至少在chrome中测试过)。

try the window.location.replace:

尝试window.location.replace:

if (!window.location.hash) 
    {
        window.location.replace(window.location.href + "?single")
    } 

#4


2  

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

我更喜欢窗户。位置= " ?单”;或窗口。位置+ =”?单”;

#5


2  

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.

因为你的url中只有一个“。com”,它会用。我只是添加了varAppend,以防您希望以后用不同类型的url变量修改代码更容易一些。

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

另一个注意事项:.replace工作通过向href添加,因为href返回包含完整url地址信息的字符串。

#6


2  

You can use Javascript URLSearchParams.

您可以使用Javascript URLSearchParams。

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

#7


1  

var singleText = "single";
var s = window.location.search;

if (s.indexOf(singleText) == -1) {
    window.location.href += (s.substring(0,1) == "?") ? "&" : "?" + singleText;
}

#8


1  

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

单击这些链接,可以看到这些更加灵活和健壮的解决方案。他们回答了一个类似的问题:

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.

这些允许您以编程方式设置参数,并且与本问题中建议的其他技巧不同,对于已经具有参数的url,或者如果您认为可能发生的其他事情不太一样,这些技巧不会中断。

#9


0  

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

我用Jquery的Load来处理这个,对我来说很有用。查看我的项目代码。我需要刷新参数,将Javascript变量放入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;
}