如何通过向 URL 传递参数来使用 jQuery 刷新页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5233050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to refresh a page with jQuery by passing a parameter to URL
提问by KillerFish
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
如果我的网址是www.myweb.com
,我想通过传递这样的参数来刷新它
www.myweb.com?single
Thank you
谢谢
回答by Mark Coleman
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";
}
回答by Rui Seixas Monteiro
回答by Fábio N Lima
You can use Javascript URLSearchParams.
您可以使用 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:
[更新]:如果需要 IE 支持,请查看此线程:
SCRIPT5009: 'URLSearchParams' is undefined in IE 11
SCRIPT5009:“URLSearchParams”在 IE 11 中未定义
Thanks @john-m to talk about the IE support
感谢@john-m 谈论 IE 支持
回答by Eyal Ch
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.hash
为空,则不能在不使用正确函数的情况下为 location.href 分配新值(至少在 chrome 中测试过)。
try the window.location.replace
:
试试window.location.replace
:
if (!window.location.hash)
{
window.location.replace(window.location.href + "?single")
}
回答by Ori
Concision counts: I prefer window.location = "?single";
or window.location += "?single";
简洁很重要:我更喜欢window.location = "?single";
或window.location += "?single";
回答by sksallaj
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.
由于您的网址中只有一个“.com”,因此它只会替换.com
为.com?single
. 我只是添加了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 地址信息的字符串。
回答by Michael Scheper
Click these links to see these more flexible and robust solutions. They're answers to a similar question:
单击这些链接可查看这些更灵活、更强大的解决方案。他们是对类似问题的回答:
- With jQuery and the queryplug-in:
window.location.search = jQuery.query.set('single', true);
- Without jQuery: Use
parse
andstringify
onwindow.location.search
- 使用 jQuery 和查询插件:
window.location.search = jQuery.query.set('single', true);
- 没有 jQuery:使用
parse
和stringify
打开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.
这些允许您以编程方式设置参数,并且与针对此问题建议的其他 hack 不同,不会破坏已经具有参数的 URL,或者如果其他事情与您认为可能发生的情况不同。
回答by Kris Ivanov
var singleText = "single";
var s = window.location.search;
if (s.indexOf(singleText) == -1) {
window.location.href += (s.substring(0,1) == "?") ? "&" : "?" + singleText;
}
回答by Berry van Zanten
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;
}