do something if window.history location is from www.example.com - javascript

how can we determine if window.history is from a certain URL like www.example.com , and then do something for example :
if ( //window.history includes example.com string ) {
$('.myclassname').hide()
}
it actually needs to know the link location of history ... is there any way to find it out ?

You can only get the referer(the page user came from) via document.referer. You can't access history from js.

Related

How to prevent facebook 301 redirect when using window.open?

I am using the facebook business extension and have the following bit of code in my FBE integration::-
initFaeClient() {
// Handling the case for closing all open FAE client as suggested in DOC
if (this.clientWindow) { this.clientWindow.close() }
this.clientWindow =
window.open(this.fetchClientUrl(), this.clientWindowName, utill.setWindowOptions())
}
Basically this.fetchClientUrl() is going to return the following :
http://www.facebook.com/ads/dia?origin=http%3A%2F%2Fmytestsite.test
The problem is when this code runs from a mobile device and hits the above url , facebook does ( I believe ) a 301 redirect and then the url changes to:-
http://m.facebook.com/ads/dia?origin=http%3A%2F%2Fmyshopmatic.test
I don't wont this to happen , is there any way i can prevent facebooks 301 redirect ? the FBE api does't work with the m.facebook.com url and i've spoken to a facebook rep about this, so how do i prevent this redirect ? anything i can pass inside the window.open method ?

Getting the current domain name in Chrome when the page fails to load

If you try to load with Chrome: http://sdqdsqdqsdsqdsqd.com/
You'll obtain:
ERR_NAME_NOT_RESOLVED
I would like, with a bookmarklet, to be able to get the current domain name and redirect it to a whois page in order to check if the domain is available.
I tried in the console:
window.location.href
but it outputs:
"data:text/html,chromewebdata"
Is there any way to retrieve the failed URL?
The solutions given by others didn't work (maybe because I was getting a different error or have a newer version: Chrome 55):
document.querySelector('strong[jscontent="hostName"]').textContent
but the same can be achieved via:
document.querySelector('#reload-button').url
A potentially more future-proof version (from Thomas's comment)
loadTimeData.data_.summary.failedUrl
So a cross-version solution incorporating all workarounds:
var url = (l‌​ocation.href === 'data‌​:text/html,chromeweb‌​data'
&& loadTimeData.data_.summary.failedUrl
|| document.querySelector('#reload-button').url
) || location.href;
var hostname = (l‌​ocation.href === 'data‌​:text/html,chromeweb‌​data'
&& loadTimeData.data_.summary.hostName
|| document.querySelector('strong[jscontent="hostName"]').textContent
) || location.hostname;
On the Chrome error page, location.href doesn't point to the domain you tried to visit, since it's an internally-hosted page.
However, the domain name you tried to visit is available if you expand the "Show Details" link.
You can run this code in console (or a bookmarklet) to parse out the domain name:
document.querySelector('strong[jscontent="hostName"]').textContent
A modified version of nderscore's since you'll need to have an if statement for the return of the correct one.
function getUrl () {
if(window.location.hostname == "") {
return document.querySelector('strong[jscontent="hostName"]').textContent
} else{
return window.location.href;
}
}
While looking in source code of html page using Developer Tools (right-click on a web page, and select Inspect Element), I've found that full original failed URL is in a variable called
loadTimeData.data_.summary.failedUrl
In my case I have to update some 'incorrect part' of auto generated URL to 'correct part'. So I've created bookmarlet like this:
javascript: location.assign(loadTimeData.data_.summary.failedUrl.replace('IncorrectPart','CorrectPart'));

Check if homepage using window.location

Is it possible to check if I'm on the homepahge/index of a site using window.location?
I'm currently checking url using
window.location.href.indexOf("/category/something")
but how can I check for homepage? It doesn't contain any segments.
Note: I don't know what the homepage URL will be so I can't use for example a url name like window.location.href.indexOf("myhomepage.html")
Update: The only clue that I have is that the homepage has no URL segments.
The term homepage itself is a fairly vague construct and not something that is technically identifiable. You could have several different landing pages depending on your screen size, device, credentials, browser, date/time, geolocation, etc. etc.
The only way you can ensure you are on one of these landing pages is to be in control during the initial GET request to the domain (e.g. http://www.example.com).
So, if you're already on the page, there's no real way to know how you got there and if this is the default page provided from that domain, though there are hacks you could try to get a general (albeit very error-prone) idea.
For example, you could compile a list of common homepage paths:
var homepages = [ '/', 'index.html', 'index.htm', 'index.php', 'main.html', 'main.htm', 'homepage.html', 'index2.htm' ];
Then compare to the provided window.location.pathname:
if (homepages.indexOf(window.location.pathname) >= 0) {
// we might be on a "homepage"
}
Many site's homepage, including stackoverflow contain a link to that same homepage.
// browser url = http://example.com
my site
If you have access to the source, you can identify this link server-side
<a id="homepage" href="http://example.com"/>my site</a>
So, to check if you are on the homepage:
document.addEventListener('DOMContentLoaded', function(e) {
var link = document.querySelector('#homepage');
if (link.href == window.location.href) {
//i'm on the homepage
}
})
So i just saw your update to the question. if you know there are no url segments, wouldn't window.location.pathname always be "/"
If you simply did window.location and checked the args in there, if there are no pages/paths appended, url matches origin, etc., would that not be good enough to detect -- Idk, I've never had a need to do this? This might also work for .NET and 'other' types of HTML naming conventions (i.e. index.html vs index.htm). Additionally, if someone changes the doc root, or a home page pointer, you'd more or less know (aka not care) because window.location you can check the following:
window.location
Location {replace: function, assign: function, ancestorOrigins: DOMStringList, origin: "http://stackoverflow.com", hash: ""…}ancestorOrigins: DOMStringListassign: function () { [native code] }hash: ""host: "stackoverflow.com"hostname: "stackoverflow.com"href: "http://stackoverflow.com/"origin: "http://stackoverflow.com"pathname: "/"port: ""protocol: "http:"reload: function reload() { [native code] }replace: function () { [native code] }search: ""toString: function toString() { [native code] }valueOf: function valueOf() { [native code] }__proto__: Location
which has path = '/'. That's pretty indicative of homepage'ish'ness.
A JavaScript knows only it's current context. It has no idea where in a site's hierarchy it is, so no, you can't check if you're on the homepage without already knowing the homepage's URL

Javascript Get Website URL

How do I get Javascript to tell me the website url.
For example if I have a page www.example.com/page.html
I want Javascript to tell me the site url is www.example.com and not www.example.com/page.html (which document.location tells me)
Is there a way to do this? If so, how?
Thanks in advance for your help :)
There are several ways you can do this, but one way might be best for certain situations (e.g. within an iFrame).
Protocol + Domain + Page
document.URL
> "http://example.com/page1.html"
document.location.href
> "http://example.com/page1.html"
Protocol + Domain
document.location.origin
> "http://example.com"
Domain
document.location.host
> "example.com"
Page
document.location.pathname
> "/page1.html"
There are many ways to get this.
Open Chrome browser and press F12, you'll get console.
Type following commands there for the same question URL. You will get your answer
window.location.hostname // Output : stackoverflow.com
window.location.origin // Output : http://stackoverflow.com
document.location.host // Output : stackoverflow.com
Use
window.location.hostname
You can test it by just typing it in the chrome dev tools console
Reference
MDN: https://developer.mozilla.org/en-US/docs/Web/API/Location
use
document.location.origin+document.location.pathname;
where document.location.origin will redirect you to "http://www"
and document.location.pathname will redirect you to "/stackoverflow/"(Name of your project).
In this way you can give reference to page or post you want in your js file.Suppose if i want reference to my home page i would have use
var address=document.location.origin+document.location.pathname;
window.location.replace(address+"/home");
So using above example i can easily redirect to my homepage
Try this
document.location.host
Use alert(window.location.origin) for getting the url.
Try
document.location.origin
That will give you the protocol and host.
you can also use location.href = '/' + 'path_name/sub_path_name'
'/' = takes you to the home page then
'path_name/sub_path_name' = to pass the new path to the domain page

Retrieve a cookie from a different path

My current document URL is http: //127.0.0.1/foo and I need to change the value of a cookie for http: //127.0.0.1/bar.
document.cookie is empty because document's URL is foo.
For the moment, I just want to read the cookie value.
Any clue?
When you create the cookie, if you set the path to '/' instead of 'foo' you will be able to read it anywhere on the domain, including '/foo', '/bar', etc.
You can create an <iframe> pointed at a resource inside /bar, and cross-frame-script into it. eg:
<iframe src="/bar/blank.html" id="barframe"></iframe>
var barframe= document.getElementById('barframe');
var bardocument= 'contentDocument' in barframe? barframe.contentDocument : barframe.contentWindow.document; // IE compat
alert(bardocument.cookie);
Cookie path= is a convenience measure to prevent accidental cookie name clashes. Given that different paths share a JavaScript origin, it is not an effective security mechanism.
You can't access cookies from a different path - otherwise it would be a security hole.
The only way I can think of is making /bar set a cookie whose path=/ so that all pages in / (including /foo) could access it.
As JJ and grawity have mentioned there is no way you can do this from your page. However, you have a work around.
i. Place an iframe which points to http://localhost/bar. Have a hidden element on the "bar" page where you store the cookie value. (let this iframe be 1*1 size so it is not visible).
ii. Use JavaScript on "foo" page to fetch the cookie value.
A similar approach (with modifications) can be used to write the cookie value too!
Thanks,
Ramjee.

Categories

Resources