I have a question on how to detect a domain url using Javascript and redirect url.
My point is to redirect url if the url is not my domain. (Eg. my domain is website.com. if the domain is not website.com, it will redirect to website.com.)
I think this will help me against Httrack or other web copier. This will redirect their url if my file is on their site.
Thank in advance.
You can get the current DOMAIN (without any other paths etc)
window.location.hostname;
You can then 'redirect' to another URL using
// similar behavior as an HTTP redirect
window.location.replace("http://stackoverflow.com");
// similar behavior as clicking on a link
window.location.href = "http://stackoverflow.com";
// Another method for doing redirecting with JavaScript is this:
window.location = "https://stackoverflow.com";
Obviously you'll need to carry out a comparison against your own URL before you carry out the redirect, so :
if (window.location.hostname !== "www.mywebsite.com"){
window.location = "https://www.mywebsite.com";
}
Related
Lets say I have a URL like this one
let RURL = 'https://out.psychologytoday.com/us/profile/398490/website-redirect?_ga=2.242270594.1285330155.1596203882-512347135.1596038085'
and its redirect to this URL https://www.sinclairmethod.org/
Now how can I get the original URL to which it is getting redirected?
Using javascript from the redirected page, you can get the referer URL from:
document.referrer
More info: https://developer.mozilla.org/en-US/docs/Web/API/Document/referrer
I have a url having Basic Authorization applied to it. I need to redirect to this url from another url and need to pass the credentials in the header. I can use jquery/javascipt/mvc or any other way to do so.
I tried using window.open(), location.replace(), $.ajax() etc but I am not able to assign any headers in these.
Is there any way to redirect to url or open a url in separate window and pass the authorization header in the request?
I've been playing with 301 redirection with masking with the purpose of sending newdomain.com to hostingdomain.com without the user noticing (transparent redirection). However all available referrer data seems to be missing. For instance I cannot use:
<?php echo parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST); ?>
or
document.getElementById("h1").innerHTML = document.referrer; // nothing
document.getElementById("h1").innerHTML = location; // domain hosting files
They all give me hostingdomain.com's (the domain hosting the files) URL data. Please help me come up with a solution to transparently send my domains to my one hosted domain and be able to grab the original domain's data for use in heading tags and such.
So there is a parent page on one domain that includes an iframe from a different domain.
I am trying to navigate parent page from the iframe using a relative path that would take base url from the parent.
I tried with
<script type="text/javascript">
function navigateParent(targetPage) {
var url = window.parent.location.href;
if (url.indexOf('.') > -1) {
url = url.substring(0, url.lastIndexOf('/') + 1);
}
window.parent.location = url + targetPage;
}
</script>
but this produces an error:
Unsafe JavaScript attempt to access frame with URL ...my parent url... with URL ...my iframe url... . Domains, protocols and ports must match.
Is there any way to do this? Ie specifiying some cross-domain permission or something.
Cross domain protections are now serverly enforced browser-side to protect the user (following some cross domain injections).
To call a function or change a property in a frame/window served by another domain you can
specify CORS headers on the server (this tells the browser that cross domain communication is fine)
preferred : use postMessage to communicate between windows and frames
i am developing facebook application, and i need to get the facebook root page url not iframe, all these methods: document.location,window.location and top.location... return the iframe url ( my hosting server ) not facebook url.
any suggestions ?
Try parent.location.href. It might not work if the iframe isn't coming from the same domain, and if so you will just need to get the request url referrer with your server code.
Edit: To get the current url in PHP try $_SERVER['HTTP_REFERER']