Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript pushState on a subdomain throws exception

Tags:

javascript

In my js file:

window.history.pushState('','',slug);

I know about security restrictions for the pushState method:

The new URL must be of the same origin as the current URL; otherwise, pushState() will throw an exception.

However, in my website I use a domain: www.mydomain.com where pushState works fine. But when I call the method on my subdomain subdomain.mydomain.com, it throws a weird exception:

Uncaught SecurityError: Failed to execute 'pushState' on 'History': A history state object with URL 'http://0.0.7.210/' cannot be created in a document with origin 'http://subdomain.mydomain.com'.

I do call the IP 0.0.7.210 as something internal but I get this exception on development + live environment.

I do resolve my subdomains via Route53 by the way. Maybe it has to do with that?

like image 773
DonMB Avatar asked Jul 23 '15 13:07

DonMB


1 Answers

It turned out that pushing an additional /solved the issue.

Changed this

window.history.pushState('','',slug);

to this

window.history.pushState('','','/'+slug);
like image 64
DonMB Avatar answered Nov 15 '22 17:11

DonMB