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

JqueryUrlParameter Passing

Jquery Problem Overview


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 to URL.

How can I do this by using jQuery?

Ex:

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

  www.myweb.com?single

Thank you

Jquery Solutions


Solution 1 - Jquery

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

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

Solution 2 - Jquery

I would use REGEX with .replace like this:

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

Solution 3 - Jquery

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:

https://stackoverflow.com/questions/45758837/script5009-urlsearchparams-is-undefined-in-ie-11

Thanks @john-m to talk about the IE support

Solution 4 - Jquery

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

Solution 5 - Jquery

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.

Solution 6 - Jquery

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")
    } 

Solution 7 - Jquery

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.

Solution 8 - Jquery

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

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

Solution 9 - Jquery

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;
}

Solution 10 - Jquery

I used this exact thing before, I have a custom function that I used for the if statement like mentioned by Mark Coleman aswell as submitted a shorter version of his answer but just for the kicks, here is my version

function isOfType(sParam) {
   var sPageURL = window.location.search.substring(1);
   var sURLVariables = sPageURL.split('&');
   var sParameterName = sURLVariables[0];
   return (sParameterName.toLowerCase() == sParam)
}

once you have that function you will use it like this

if(!isOfType('single'))
    window.location.href += '?single';

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKillerFishView Question on Stackoverflow
Solution 1 - JqueryMark ColemanView Answer on Stackoverflow
Solution 2 - JqueryRui Seixas MonteiroView Answer on Stackoverflow
Solution 3 - JqueryFábio Nicolau de LimaView Answer on Stackoverflow
Solution 4 - JqueryOriView Answer on Stackoverflow
Solution 5 - JquerysksallajView Answer on Stackoverflow
Solution 6 - JqueryEyal ChView Answer on Stackoverflow
Solution 7 - JqueryMichael ScheperView Answer on Stackoverflow
Solution 8 - JqueryKris IvanovView Answer on Stackoverflow
Solution 9 - JqueryBerry van ZantenView Answer on Stackoverflow
Solution 10 - JqueryJean PierreView Answer on Stackoverflow