I have a script that adds an param to the url when I click the assigned button - next click replaces it with a new param - this work great.
However - now I have three buttons - and I want each button to assign a param to the url - and replacing any params added by any of the other buttons. It also needs to be placed last behind the params that are already there.
so:
(button1) click3: /m4n?ecom-query=imac&seid=etailer-products&viewMode=3?param=grid
(button2) click4: /m4n?ecom-query=imac&seid=etailer-products&viewMode=3?param=list
(button3) click5: /m4n?ecom-query=imac&seid=etailer-products&viewMode=3?param=smalllist
The url before ?param is dynamic and can look different.
$('.click2').on('click', function() {
console.log("Clicked");
var url = window.location.pathname;
var url = window.location.href;
if (url.indexOf('?param=list') > -1) {
url = url.replace("?param=list", "") + '?param=grid'
} else {
url = url.replace("?param=grid", "") + '?param=list'
}
window.location.href = url;
});
How do I do this, I tried to modify my existing script but had no luck.
I think there is a small error in your approach:
All parameters in url should be connected with an &
so now your url should look like that
/m4n?ecom-query=imac&seid=etailer-products&viewMode=3¶m=grid
now if you want to replace old pram, you need to remove the old value also. For that you can use regex as in following code
url = url.replace(/\¶m=.*/,'') + '¶m=list'
So the full code would be:
$('.click2').on('click', function() {
console.log("click2 Clicked");
var url = window.location.href;
url = url.replace(/\¶m=.*/,'') + '¶m=list';
window.location.href = url;
});
Hope it helps
Related
I need a button, which lead up in url path like this:
from /host/elements/1
to /host/elements
I have a link in my html:
And a javascript function:
function go_backward() {
var url = window.location.href;
var page = url.substring(0, url.lastIndexOf('/'))
window.location.assign(page);
return false;
}
But when i click the button i only get desired url printed, without correct redirect.
Why cat it be so?
Change:
window.location.assign(page);
return false;
to
window.location.href = page;
If you just want to go back you can also use:
window.history.go(-1)
I need all blog product pages to show in a popup. In order to show in the popup their url must be in the form https://expample.com?/modal-link=blog_page_url. (I'm using the plugin and this is the requirement)
I would like to write a code in javascript that checks the URL. If the URL of the page contains the word 'product' I would like prepend to the url: https://expample.com?/modal-link= inorder to enable it to be shown in a popup.
I'm using the code below:
if(window.location.href.indexOf("product") > -1) {
var url = window.location.href;
url_new = 'https://example.com/?modal-link=' + url
} else {
}
window.location.href = url_new;
The is creating a new URL but it is causing it to be added an infinite amount of time.
How should I be doing this?
Follow on question: (should I open a new question for this?)
I would like to adapt the code so the page does not reload during the redirect.
I know there are other posts about this eg How do I modify the URL without reloading the page? or https://stackoverflow.com/questions/3338642/updating-address-bar-with-new-url-without-hash-or-reloading-the-pagebut could someone please help me modify my javascript code for this specific case?
Would I need to use the lines below?
document.location.hash = 'afterhash';
history.pushState('data to be passed', 'Title of the page', '/test');
I'm at a loss which part of my code need to go where in the above lines.
Your recursion is missing a stop condition. For example, if "some_product" contains "product" and you prepend anything to it, it will still contain "product", as in "really_some_product", "really_really_some_product", etc. You can see where this is going, infinite recursion.
So, you need to tell it to stop at some point, which is when the new url already starts with what you intend to prepend to the original one.
Following this, since there's a case in which we don't change anything, we should also not redirect.
var url = window.location.href,
prepend_to_url = "https://example.com/?modal-link=",
url_new = false;
if (url.indexOf(prepend_to_url) == 0) {
// url starts with what we prepend
// so do nothing
} else if(url.indexOf("product") > -1) {
url_new = prepend_to_url + url;
}
if (url_new) { // don't redirect unless we've done something above
window.location.href = url_new;
}
A more concise version of the code above could look like this:
var url = window.location.href,
prepend_to_url = "https://example.com/?modal-link=",
url_new = false;
if (url.indexOf(prepend_to_url) == -1 // url doesn't start with what we prepend
&& url.indexOf("product") > -1 // and our condition is met
) {
url_new = prepend_to_url + url;
}
url_new && (window.location.href = url_new); // equivalent to an "if" statement
What you need is to get the query parameter part of the url by using substr with index of ? to the end of the url
var url_new;
if(window.location.href.indexOf("product") > -1) {
var url = window.location.href.substr(window.location.href.indexOf("?") +1, window.location.href.length);
var newValue = 10;
url_new = 'https://example.com/?modal-link=' + newValue + "&" + url
}
console.log(url_new);
You should initilize the url_new and change it for some condition:
let url_new = window.location.href
if(window.location.href.indexOf("product") > -1) {
url_new = 'https://example.com/?modal-link=' + window.location.href;
}
window.location.href = url_new;
I have a sample page, let' say testpage.pl When I choose English version, GET parameter is added to URL, like /?language=en.
Afterwards, when I click menu positions, they are in the English version so everything is OK.
But if I want to have English version of a subpage directlty after pasting URL in a browser, like
http://testpage.pl/wyjazdy-i-przyjazdy/erasmus-incoming-staff/accommodation.html)
the Polish version is opened. So I've made a simple redirect function like below, but it comes to the loop after first start. This function redirect to the same page, but before it tries to redirect to this first URL with GET parameter ?language=en
How to solve this?
function cleanUrl() {
window.location = "http://testpage.pl/?language=en";
var cleanedUrl = "http://testpage.pl/wyjazdy-i-przyjazdy/erasmus-incoming-staff/accommodation.html";
var currentUrl = window.location.href;
if (currentUrl !== cleanedUrl) {
window.location = cleanedUrl;
}
}
cleanUrl();
Your are updating url in first line of function which is causing your code to loop infinite. Remove that line or move to some other function for fix
function cleanUrl() {
var cleanedUrl = "http://testpage.pl/wyjazdy-i-przyjazdy/erasmus-incoming-staff/accommodation.html";
var currentUrl = "http://testpage.pl/?language=en";
if (currentUrl !== cleanedUrl) {
window.location = cleanedUrl;
}
}
cleanUrl();
Keep the window.location assignment as last operation.
function cleanUrl() {
var enUrl = "http://testpage.pl/?language=en";
var cleanedUrl = "http://testpage.pl/wyjazdy-i-przyjazdy/erasmus-incoming-staff/accommodation.html";
var currentUrl = window.location.href;
if( currentUrl !== cleanedUrl ) { enUrl = cleanedUrl; }
window.location = enUrl;
}
I have a button on the page which reloads the page based on some value selected. The button's onclick event of calling load_newurl(param).
function load_newurl(param) {
var url = window.location.href;
var index = url.indexOf("&test=");
if (index>=0) {
url = url.substring(0, index);
}
url = url + "&testrun=" + param;
window.location.href = url;
window.location.reload();
}
Above is my function to reload the page. However, window.location.href never gets changed. Do anyone know why? Am I doing something wrong...?
Thank you in advance.
Don't call reload.
This should work, provided there's nothing else wrong with your code.
function load_newurl(param) {
var url = window.location.href;
var index = url.indexOf("&test=");
if (index>=0) {
url = url.substring(0, index);
}
url = url + "&testrun=" + param;
window.location.href = url;
}
Just remove the call to
window.location.reload();
it should work.
Your code works fine, but while the href is about to be set, the reload() refreshes the current page and its href stays the same.
Just try your code ommiting window.location.reload();.
I'm unable to work out where I'm going wrong with the following code.
My full url is http://localhost:4244/Invoice/PayInvoice?invoicenumber=7069 but var url = window.location.href; only returns http://localhost:4244/
<a Class="btn btn-success btn-sm confirm" href="/Invoice/PayInvoice?invoicenumber=7069">Pay Invoice</a>
Can anyone spot what I'm doing wrong
<script>
$(document).ready(function() {
$(".confirm").click(function (e) {
var url = window.location.href; // Returns full URL
alert(url);
$("#displayPopUp").show();
e.preventDefault();
$(".yesupdate").click(function () {
$("#displayPopUp").hide();
window.location = url;
//$(location).attr('href');
});
//if (AcceptPayment()) {
// // then redirect to original location
// window.location = this.href;
//}
//else {
// alert("Couldn't do my thing first");
//}
//var result = window.confirm("You are about to mark invoice as paid, are you sure?");
//if (result == false) {
// e.preventDefault();
//}
});
});
</script>
What I'm trying to do is pass the value of url to window.location = url; when .yesupdate is clicked
If you're trying to navigate to the href of the <a> element being clicked, you can retrieve that address within the .click() handler using:
var url = this.href;
The lines in your snippet just assign the current location back to itself, which will just reload the page.
var url = window.location.href;
window.location = url;
You may also need to remove previous click handlers from $(".yesupdate") to avoid them stacking from multiple .confirms.
You can use namespaces to target certain handlers.
$('.yesupdate').off('.confirm').on('click.confirm', function (e) {
// ...
});
You should not assign to window.location, it is a read-only instance of a Location object. Depending on your browser, this may or may not work or throw.
Instead, you should call window.location.assign(url) with the URL you would like to navigate to. You can use a full URL with query string and other parts.