I have a local host called "myproject". When i try to set a cookie for resource "myproject/someresource" like this:
document.cookie = "mycookie=somevalue; path=/someresource";
IE does not set this cookie. But it does if i do not use path parameter:
document.cookie = "mycookie=somevalue";
What am i doing wrong?
according http://forum.de.selfhtml.org/archiv/2009/7/t189018/ the site must be within the specifed path. So I think you have to add a slash at the last position and you site must be in "http://mydomain.com/somesource/filename.datatype"
Related
How to I use a JQuery value for a url to use with location.href?
The url will be different every time the below url is an example.
The URL is stored in the database as
audit.php?audit=13957911461655047299&page=summary
Ajax is then used to retrieve the URL and save in a var called last_viewed so I would like to use the equivalent of
location.href = last_viewed
I have tried
location.href = '"'+last_viewed+'"'
but the URL becomes
http://www.x-rayqa.co.uk/"audit.php?audit=13957911461655047299&page=summary"
which obviously won't work because of the extra "s
if I try just
location.href = last_viewed
nothing happens, the script is broken.
If you don't add anything to the URL in your example, the browser assumes it's a relative URL and prefixes it with the current location origin (http://www.x-rayqa.co.uk on your site). If that stored link is on the same server, you'll need to ensure the path is correct after the server name.
If it's not local (i.e., an external link) then you'll have to add the server and protocol prefix yourself to make the link work. Take a look at the window.location documentation as well, which might help clear some things up.
https://developer.mozilla.org/en-US/docs/Web/API/Window.location
You can assign directly to window.location.href or use window.location.assign().
I am using the window location method to redirect a webpage to another after a set amount of time.
The url needs to change from www.myurl.com/home to www.myurl.com/other. The problem is that I do not know what the final URLs will be so I cannot use absolute links, they have to be a path only. This is what I have so far:
window.location.pathname = "mobility.html"
You can just prepend a / to your URL to make them relative to the domain root (without having to hardcode the domain name). Like this:
window.location = "/mobility.html"
window.location.assign("/path") also works.
I have a website on which I dynamically create Javascript code using ASP.NET handler in which I should add the referrer to a database.
I want to get referrer of referrer like so:
website1
website2 (where I create pixel to another site)
website3 (where pixel is located)
I don't have code access to website1, on website2 I can only assign JavaScript.
If I get referrer in current application state I get website2.
Is there a way to get website1 as referrer?
You can pass this value along: document.referrer.
That expression would need to be evaluated on website 2, not on website 3.
So:
// website2.html
<img src="website3.com/pxl.gif" id="pxl" />
<script>
document.getElementById('pxl').src += '?ref=' + encodeURIComponent(document.referrer);
</script>
The request to website3 will then include the referrer.
It is impossible to get the referrer of website2 on website3 directly. However, since you can use javascript on website2, you could get the referrer (document.referrer) and add it to the url of the pixel you get. For example:
var referer = document.referrer;
var pixelUrl = 'http://website3/pixel?referrer=' + escape(referrer);
// create pixel...
Hope that helps
Seems that document.referrer doesn't work in many instances.
Use the complete window.frames.top.document.referrer instead.
I have a srcURL variable which gets a path of the form /myFolder/myFile.jpg
Now this gets assigned to the img element..which obviously would call it with the complete path https://mySite.com/myFolder/myFile.jpg
Now I somehow want the https to be replaced/enforced with http using Javascript..
I am not sure if I can do this with the "replace()" method since I only get the path "/myFolder/myFile.jpg" in the srcURL variable and not with https..
How can I do that?
You are using a relative path. You need to use an explicit path when setting the src of the URL.
srcURL = '/myFolder/myFile.jpg';
srcURL = 'http://' + window.location.host + srcURL;
// srcURL == 'http://<yourdomainname>/myFolder/myFile.jpg'
Note: you'll probably get a warning message saying some parts of your page may be unsecure.
If you want to enforce plain HTTP, you should write a rewrite rule on the server to forward any HTTPS request for an image to the HTTP equivalent. On the client side, simply doing this would be sufficient (but you really need the back end piece too):
url.replace("https", "http");
I am wondering how I would get JavaScript to check if a user is on a certain URL so i can build an if statement from the result.
My reasoning is that if a user clicks on a link in the menu and they are currently on trucks.php the javascript will redirect them to a certain page. If they are not on trucks.php they will be directed to a different page.
Cheers guys.
The current location is in location.href.
The location object also contains some other useful fields:
location.hash: The part after the # in the URL
location.host: Hostname including port (if specified)
location.hostname: Just the hostname
location.pathname: The requested URI without protocol/host/port; starting with a /
location.port: The port - only if one is specified in the URL
location.protocol: Usually 'http:' or 'https:' - mind the colon at the end
In your case the most fail-safe way to check if the filename is trucks.php is this:
var parts = location.pathname.split('/');
if(parts[parts.length - 1] == 'trucks.php') {
location.href = 'some-other-page';
}
If you want to redirect without keeping the current page in history, use the following code instead of the location.href assignment:
location.replace('some-other-page');
Use window.location.href to get the current URL, or window.location.pathname to get just the path. For your specific problem, just the path name is required for the solution:
if (window.location.pathname == "/trucks.php")
window.location = "/somewhereelse.php";
Check out the MDC documentation for window.location.
Use window.location