window.open adds page after a url not replace it - javascript

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).

Related

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!

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 to get previous url including hash fragment using JavaScript?

I need to get the previous url to redirect to the previous page. I have url like www.mysite.com/users/register/#1.
I use document.referrer to get the previous url,but it doesn't return hash part(#1). How to get the previous url including hash part?
How to get previous url including hash fragment using JavaScript?
As you've noted, the hash fragment part of that means you can't use document.referrer.
If the previous page was on the same origin: You'd need to have code on that page recording the full URL, for instance in sessionStorage.
On the previous page, perhaps each time hashChange is fired:
sessionStorage.setItem("last-url", location);
On the new page, to get the URL:
var lastUrl = sessionStorage.getItem("last-url");
If the previous page was on a different origin: I'm fairly certain you can't.
I need to get the previous url to redirect to the previous page.
Actually, you don't. You can just use history.go(-1) or history.back() to do that, which work regardless of the origin of the previous page.
try for previous url,
function backtopage() {
window.history.back();
}
May be you can use onhashchange event.
When url is changed,it produces a event with old url and new url.
The oldurl has even the hash part
$(window).bind('statechange',function(){
// Prepare Variables
var State = History.getState(),
url = State.url,
states = History.savedStates,
prevUrlIndex = states.length - 2,
prevUrl = states[prevUrlIndex].hash;
});
Try this one::
In previous page url:
www.mysite.com/users/register/#1
In Current Page:
$(document).ready(function() {
var referrerUrl = document.referrer.replace("#","e");
var correctUrl=referrerUrl.replace("e","#");
});

Reload a page inside iframe

I have an iframe and I want to reload the currently displayed page on button press.
HTML:
<iframe id="webView"></iframe>
JS:
function reloadPage()
{
var webView = document.getElementById("webView");
//CODE
}
Inside the reloadPage() method I tried different solutions:
Call reload()
webView.contentWindow.location.reload();
This just doesn't work because the pages loaded inside the iframe are from a different domain than the main page.
Set src
webView.src = wevView.src;
It gives wrong result because it contains the initial url that I set to the iframe, non the current one.
Set location
webView.contentWindow.location = webView.contentWindow.location
I was expecting it to not work with urls from different domains (the same as calling reload()), but actually it works and also gives a good result.
Good but not perfect: the location object holds the current url but strips any parameter.
For example if the frame is currently displaying the following url:
http://www.myserver.com/thatsite/?page_id=11
the location object contains this url:
http://www.myserver.com/thatsite/
So this one works well as long as there are no parameters in the url.
Better solution?
I rely heavly on urls with parameters (mostly WordPress installations) so i need a way to keep them while reloading.
Anyone knows a solution to achieve this?
just not possible, see this thread:
Get current URL from IFRAME
and this one
How do I get the current location of an iframe?
Since setting location works, you could use location.search to retrieve the GET parameters and reconstruct the URL that way.
Example:
webView.contentWindow.location = webView.contentWindow.location + webView.contentWindow.location.search

Changing pages with a single link but keeping the hash part

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;

Categories