Javascript "development" and "production - javascript

I'm developing a simple application in Sinatra that makes many asynchronous calls. In my javascript code I have a variable that stores my domain for all calls. My problem is how to alter the value of the variable every time that I publish my application, for example:
var url = "http://localhost:9292"; //development
to
var url = "http://mysite.com" //production
I would like to automate this, to prevent errors in deployed application. Is it possible?
I thought about doing this in my "View" using ruby code, but do not know if it is cool.
Sorry my English.
Thanks.

Maybe you can use:
var url = 'http://' + document.domain;
document.domain get the name of the current domain.

Try with:
window.location.port
This get the domain port of the currernt website ;)

Related

Can I hook the phantomjs jumping to a new website event with python + selenium?

I use phantomjs + python selenium for testing(the reason is that phantomjs can run without GUI, which could be easily deployed on Linux), there are several separate domain pages, each has their own cookie values, such as
A.com ===> cookie_A
B.com ===> cookie_B
C.com ===> cookie_C
D.com ===> cookie_D
I can set the A.com's cookie when I build the browser,
self.profile['phantomjs.page.customHeaders.Cookie'] = cookie_A
the question is that A.com might has some javascript which will just jump to B.com or C.com or D.com, I also need to set B.com or C.com or D.com's cookie for this, what is the elegant way to do this?
I could not setup all cookies value when building the browser, as in the real case, there might be too many cookie values.
I think you need Network Monitoring, something like that:
var page = require('webpage').create();
page.onResourceRequested = function(request) {
page.addCookie( cookie_A);
};
page.open(url);

Javascript window.location.href not working if application is hosted on IIS

I have a problem regarding the javascript window.location.href function.
On the development server(Windows server 2008 with IIS) we have an asp.net application deployed in the "default web site" named Topics2 (it's a test application - the production one is Topics).
My problem is:
When i want to navigate to another page using javascript window.location.href the browser redirects me to the production page and not the test page.
Example: Instead of bringing me to server/Topics2/MyPage.aspx?id=100 it brings me to server/MyPage.aspx?id=100 (the Topics name is not shown).
There is no routing made in the application. Is IIS adding some default routing? Or could there be any routing made (that i'm not aware of) in IIS that is messing this up?
What i tried untill now:
1. window.location.href = window.location.protocol + "//" + window.location.host + "/MyPage.aspx?id=" + item.value;
2. window.location.href= "/MyPage.aspx?id="+item.value;
I know i can always hardcode the "Topics2" name but that's not wanted because we don't want to have that in mind every time we deploy to the production server.
I found the solution, which is pretty simple (shame on me that i didn't think of that!).
So, apparently if I type window.location.href= "/MyPage.aspx?id="+item.value; the server redirects me to the production server but if i type window.location.href= "./MyPage.aspx?id="+item.value; it takes me to the right page.
Try using this:
location.replace(yourUrl);

Rails removes port from local environment variable?

I have a very strange problem with my rails 4.1.0 application.
Inside local_env.yml I have a variable declared as:
API_URL: 'http://api.myapp.com:3000'
I use this variable in the javascript file app_ready.js.erb like so:
var apiHost = '<%= ENV["API_URL"] %>'
But for some reason when I call the page using this script and inspect the app_ready.js file I can see the line got converted to var apiHost = 'http://api.myapp.com' without the port :3000. Does anybody know what is happening here?
Edit:
Is there some kind of a server sided cache maybe? Because at some point I might have declared API_URL without the port and changed it later. It can't be in the browser cache, because I've actually cleared it and even tried with different browsers with the same results.
Ok, it was the cache. Had to run rake tmp:clear and restart rails server.

MVC Cookies not seen by JavaScript

Here's the JavaScript code:
var tmp = escape(document.cookie.match(/TestSession=[^;]+(;|$)/)[0]);`
With MVC, if I have this it works:
var cookie = new HttpCookie("TestSession", "SomeValue") {
Expires = DateTime.Now.AddYears(1)
};
But if I add:
cookie.Domain = "Test.com";
or
{ Expires = DateTime.Now.AddYears(1), Domain = "Test.com" };
The JavaScript has tmp as null or undefined.
Any ideas?
Remember, you cannot access cookies of another domain. So, if you're running the site under a different domain than test.com you won't be able to access the cookie.
Note: this will also be true when working from http://localhost - as localhost and test.com are not of the same origin.
To remedy the issue you could check the current host on the server and set the domain accordingly.
Update (after comment)
As noted by Andrei, you could also add a host entry to your local host file that maps test.com to localhost. Though, you'll need to remember to comment it out when you need to navigate to the production version of the site.

Can I use localhost as the domain when setting an HTTP cookie?

I am using a jQuery plugin to set cookies and when I use localhost for the domain it will not store the cookie.
Here is the plugin I am using with jQuery 1.2.6.
http://www.stilbuero.de/2006/09/17/cookie-plugin-for-jquery/
Below is the code that I am using. You can see it does not like localhost, and I am running it from a development web server on localhost. One detail is that I am running off port 4005 but that should not affect the domain, AFAIK.
$(function() {
console.log('Testing');
var one = $.cookie('Test.One');
var two = $.cookie('Test.Two');
var three = $.cookie('Test.Three');
console.log(['one', one]);
console.log(['two', two]);
console.log(['three', three]);
$('#div1').text(one);
$('#div2').text(two);
$('#div3').text(three);
$.cookie('Test.One', 'Test 1');
$.cookie('Test.Two', 'Test 2', { path: '/' });
$.cookie('Test.Three', 'Test 3', { path: '/', domain: 'localhost' });
});
I had similar problem with setting cookies. Make up a domain name and add it to your hosts file as 127.0.0.1. Then run web application on that domain.
I think the domain name of a cookie must have exactly two dots (not counting the final dot after the TLD). So .something.localhost is okay, .google.com is okay, but .localhost or google.com is not. But a glance at RFC 2965 suggests that it's more complicated than that... you might want to read that document, especially section 3.3 (and/or its precursor, RFC 2109).
I updated the jQuery plugin to not add the domain to the cookie when it is localhost. That solves my problem without touching the hosts file.
var domain = (options.domain && options.domain !== 'localhost') ? '; domain=' + (options.domain) : '';
I'm using Code Ignitor, and setting the domain to an empty string fixed my problem while working on the application on localhost. I believe this is the better solution as everyone in the development team then doesn't need to mess with their hosts files on Windows.
Production domain values can be put in the config.php of Code Ignitor when deployed on a live site.
I tried setting the host file to use an alternate name (local.acme.com) and I can now set cookies on that domain. It seems I cannot set cookies on localhost, at least not with Firefox. I do not recall that being a restriction for cookies. I would like to understand what is going on here.
Also, I did try just making the domain in the hosts file simply "dev" but that did not work. I had to use a name that ended in .com or another tld to make it work.
Simplest solution for me to resolve this was to use 127.0.0.1 instead of localhost ;-)
That works fine in Firefox!
Cookie needs to specify SameSite attribute, None value used to be the default, but recent browser versions made Lax the default value to have reasonably robust defense against some classes of cross-site request forgery (CSRF) attacks.
Along with Domain=localhost your cookie should look something like this
document.cookie = `${name}=${value}${expires}; Path=/; Domain=localhost; SameSite=Lax`;
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie/SameSite

Categories