Changing pages with a single link but keeping the hash part - javascript

there is 2 pages, 1 is ENG.html another 1 is GER.html., and there is some tabs in those 2 pages, their link is: eng.html#work eng.html#about eng.html#home
when click on change language link i need to stay on #work or #home wherever i was, how to do it? please don't post php, html and javascript and others are acceptable, and i don't want to create 2 separate (ENG, GER) folder, any suggestions?

Try this:
// set the url we're going to
var url = "GER.html", // for example
// get the hashtag text
hash = window.location.hash,
// make the new url
loc = url + hash;
// finally, go there
window.location.href = loc;

Related

window.open adds page after a url not replace it

var urladdress = 'autopage
I used window.open(urladdress, "_self"); to load a new url but it was adding urladdress to existing address within the browser i.e.
current url within the browser is https://currenturl.com/currentpage/ I want to load a new page autopage so expected url is https://currenturl.com/autopage/
but using window.open(urladdress, "_self"); leads to https://currenturl.com/currentpage/autopage/.
Please how can i achieve that
The problem stems from your "urladdress" variable.
Writing your link like this "link" will just add it to the current active path.
If you want to always end up in "www.example.com/link" just add an "/" before your path ("/autopage" in your example).

How do I disable facebook's ?brand_redir and open new page without extra piece of URL

I'm trying to disable facebook's redirection to another country page by restructuring its URL.
An example would be - you're trying to open https://www.facebook.com/VisaID/ but if you're accessing the page from another country, you're going to be redirected to the closes page based on your geolocation (I guess). In my case, it's https://www.facebook.com/VisaCzechRepublic/?brand_redir=272936492828277 and I want to prevent this.
I tried to write a piece of code to solve this, however, when I try to reload the page with restructured URL it somehow adds extra window.location.hostname and window.location.pathname resulting in an absolute mess and breaking the page...
result = https://www.facebook.com/VisaCzechRepublic/www.facebook.com/272936492828277/?brand_redir=DISABLE
Code I used:
//Split URL to get FB ID
var brand = window.location.search;
brand = brand.split("=");
brandId = brand[1];
//Create new URL
//First facebook.com
//Second Page ID
//Third add ?brand_redir=DISABLE at the end
url = window.location.hostname + "/" + brandId + "/" + "?brand_redir=DISABLE";
//Open rearranged URL
window.open(url)
I expected Chrome to open a new URL exactly as is saved in the url being https://www.facebook.com/272936492828277/?brand_redir=DISABLE
but it somehow sneaks in extra https://www.facebook.com/VisaCzechRepublic/ and only after that ads the url I want.
Thanks a ton for any advice!

Dynamically added path in URL going next to the variables. How can I move it before variables?

Just some intro:
In ecommerce template, "Symfony" based I'm loading all the products from available pages (Infinite scroll) using AJAX request. Everything working perfect when I have clear URL like this:
http://example.com/path
I'm loading products from available pages with ajax request, here some code to check (Note, not the whole functional code, but the part which affects URL):
$().ready(function(){
infiniteCollectionInit('{{ (request.url~'page1.ajax') }}');
});
function infiniteCollectionLoad(url, mode){
infiniteCollectionPage++;
url = url.replace('page1.ajax', 'page' + infiniteCollectionPage + '.ajax');
}
This simply adding page1.ajax, page2.ajax ... at the end of the URL
The problem starting at the point when filters are used in page, In that case the URL transforms to this:
http://example.com/path/?mode=grid&max=60&min=0&sort=newest
Now when scrolling down and it need to load next page's items the URL is:
http://example.com/path/?mode=grid&max=60&min=0&sort=newestpage1.ajax
Can anyone help me out to add the page1.ajax before variables like this:
http://example.com/path/page1.ajax?mode=grid&max=60&min=0&sort=newest
Thanks.
Should be something like this:
var u = new URL("http://example.com/path/?mode=grid&max=60&min=0&sort=newest")
var newUrl = u.origin + u.pathname + 'page.ajax' + u.search;
If URL is reused from the previous one than you might need to replace u.pathname with a fixed path. Otherwise you keep on adding to it.

Change current URL then load

I want jquery to find and change the current URL you are visiting and load the new one. Lets say jquery is supposed to change the current "index.html" to "indexalt.html" on click and then load it. My idea is to use document.URL to get the current URL then slice off ".html" at the end and add for example "alt.html" to string. I'm very new to jquery and don't get it to work. There will be a lot of mistakes within my script probably:
$("#contact").on('click', function() {
var url=document.URL(str.slice(-7));
.load("url +"alt.html"");
I would really appreciate if anyone can tell me how to do it and how to write it down correctly in a whole. Thanks!
location.href returns the URL of the page. So, you can do this instead.
$("#contact").on("click", function() {
// Slicing 5 characters from the URL.
const url = location.href.slice(0, -5)
// Simply load the URL
location.href = url + "alt.html"
})

How can I modify this javascript to ignore any data within a query string?

Essentially, I have this website, the content of which changes depending on what the user inputs into the query string.
When the user enters mysite.com/?1 it loads content for 1 and /?2 for 2
My problem is that I have a Facebook like button within my page, and to make it work I have written this js code:
<script type="text/javascript">
var sUrl = window.location;
document.getElementById('fbcom').setAttribute('href', sUrl);
</script>
this gets the url and allows the user to like different content from what is technically one file.
My problem is that when a user likes for example /?1 on facebook, if someone where to click this link on their newsfeed and decide that they like it too, technically they will be liking the page /?1-with all the additional facebook code on the end of the url, so heading back to /?1 the like has not registered.
How can I modify the above code to ignore any facebook rubbish on the end of the url when they are directed from facebook?
Important: the ID /?1 can be anything from a 1 digit to a 4 digit number e.g /?1234
My current JS ability is very poor. Thanks
You can combine the properties of location you actually want to keep -- which seems to be protocol, host, and pathname:
var sLoc = window.location;
var sUrl = sLoc.protocol + '//' + sLoc.host + sLoc.pathname;
You can also just use the pathname as relative-from-root:
var sUrl = window.location.pathname;
you can do that with regex:
var sUrl = window.location.toString().replace(/^(.*?)(\?.*)?$/, '$1');

Categories