How to remove url variable, then replace with new url using jQuery - javascript

I have the code below that adds the lang variable to a url each time a flag is clicked. But I need to add an additional piece - I want to remove the previous lang variable first, before adding a new one.
Currently my code keeps adding on to the previous url variable. How can I fix this?
<script type="text/javascript">
jQuery(document).ready(function($) {
$("a.flag").on('click',function() {
var lang_prefix = $(this).attr("title");
window.location.href = window.location.href + '?lang=' + lang_prefix;
});
});
</script>

If lang is the only query parameter you're using...
<script type="text/javascript">
jQuery(document).ready(function($) {
$("a.flag").on('click',function() {
var lang_prefix = $(this).attr("title");
window.location.href = window.location.href.split('?')[0] + '?lang=' + lang_prefix;
});
});
</script>

Related

How can add parameters to href url when click on tag a?

I want From home page send parameter id to carts page:
This is current code, but it not woking:
detail
<script>
function addParam()
{
var url = window.location.href;
var listId = localStorage.getItem('IDs', '');
url=url+ "?id=" +listId;
window.location.href = url;
}
</script>
How can add parameters to href url when click on tag a?
Either just override it by grabbing the element's (here done by passing this to the function) href
detail
<script>
function addParam(el)
{
var listId = localStorage.getItem('IDs', '');
window.location.href = el.href + "?id=" +listId;
}
</script>
Or append it to the element's href
detail
<script>
function addParam(el)
{
var listId = localStorage.getItem('IDs', '');
el.href += "?id=" +listId;
}
</script>

Call one function after another to get the proper url

Hi I am working in grails ..
<g:link name="searchLink" controller="MRController"
action="ExcelExport">TEST GRAILS</g:link>
<script>
$(function() {
$('a[name="searchLink"]').bind('click', function() {
$(this).attr('href', $(this).attr('href') + '?startdate=' + start().StartDate +'&enddate=' + start().Enddate);
})
})
function start(){
var StartDate = $("#startDate").val();
var Enddate= $("#endDate").val();
return {StartDate:StartDate,Enddate:Enddate}
}
</script>
I have trying to get the following url
http://localhost:8077/Myproject/MRController/ExcelExport
?startdate=2017-05-21&enddate=2017-05-23
But when i click the link again i get the following url instead i want the link with new startdate and enddate value
http://localhost:8077/Myproject/MRController/ExcelExport?
startdate=2017-05-21&enddate=2017-05-23?startdate=2017-05-21&enddate=2017-05-23
Edited
I have tried with the following script to clear the parameters in the first call but i don't know how to make the script run one after another means first the url call work then the parameters will clear through the function
<script>
function refineUrl()
{
//get full url
var url = window.location.href;
//get url after/
var value = url.substring(url.lastIndexOf('/') + 1);
//get the part after before ?
value = value.split("?")[0];
return value;
}
</script>
Don't change the original href attribute in your JS-code:
$('a[name="searchLink"]').bind('click', function() {
document.location = $(this).attr('href') + '?startdate=' + start().StartDate +'&enddate=' + start().Enddate;
})

JS var declared outside <script> file for use in said file

I have a bit of JS added to my home page like so-
<script type="text/javascript">
var mm_home;
mm_home = true;
</script>
I have another JS file that is being pulled into the head (below the above) that has the following lines:
baseUrl = $(this).parent().attr("action");
if(mm_home) {
url = baseUrl + 'category/bank-events/' + $('#tribe-events-events-year').val() + '-' + $('#tribe-events-events-month').val();
} else {
url = baseUrl + $('#tribe-events-events-year').val() + '-' + $('#tribe-events-events-month').val();
}
However, when I view my home page, the above conditional is failing and it reverts to the 2nd url var when I want it to use the first.
What am I doing wrong here?

redirect to same site, but different link (javascript)

I want that people in "example.com/index.htm" get redirected to "example.com".
but the code is being tricky. It always takes me to a loop.
<script type="text/javascript">
function check()
{
if (window.top!="example.com")
{
window.location="http://example.com";
}else {}
}</script>
<script type="text/javascript">
function check()
{
if (window.top.location.href!="http://mysite.com")
{
window.top.location.href="http://mysite.com";
}else {}
}</script>
You need to use the location object and it needs to actually match.
Give this a go (untested):
$(function() {
var reg = /index.htm/g,
top = window.location.href,
loc = '';
if(reg.test(top)) {
top = top.replace('http://','');
top = top.split('/');
for(i=0;i<top.length-1;i++) {
loc = loc + top[i];
}
window.location.href = 'http://' + loc;
}
});

How to pass javascript variables from one html page to other html page

I have the following code to pass variable from example1.html to example2.html , what will be the syntax in window.location.href to navigate to example2.html with username and keyword.
example1.html
<script type="text/javascript">
var username = ($("#username").val());
var keyword = ($("#keyword").val());
$("#button1").click(function(){
window.location.href = "http://localhost:2757/example2.html";
});
</script>
If you want to pass them by query string parameters you can do this:
window.location = "http://localhost:2757/example2.html?username=" + username + "&keyword=" + keyword;

Categories