window.location.href relative paths

5,282 views
Skip to first unread message

Trindaz

unread,
Jun 27, 2012, 5:20:16 AM6/27/12
to phan...@googlegroups.com
Is it a bug that doing

window.location.href = '/my_relative_path'

gives me

Not allowed to load local resource: file://phantom1.js/search?q=test&s3=t30

?

Trindaz

unread,
Jun 27, 2012, 5:22:53 AM6/27/12
to phan...@googlegroups.com
(my bad - that's a copy paste error. Should have been)

Not allowed to load local resource: file://phantom1.js/my_relative_path

Ariya Hidayat

unread,
Jun 27, 2012, 2:35:05 PM6/27/12
to phan...@googlegroups.com
Is the specified path under the path of the current URL?



--
Ariya Hidayat, http://ariya.ofilabs.com
http://twitter.com/ariyahidayat

Dave Trindall

unread,
Jun 27, 2012, 8:55:44 PM6/27/12
to phan...@googlegroups.com
It would have to be - it's not even running code I've written. I'm browsing a web app that works fine in Chrome when it does its own changes to window.location but breaks when I do exactly the same commands in Phantom. My current workaround is to literally overwrite the app's own javascript functions that make changes to window.location so that any window.location assignments look like:

window.location.href = window.location.host + relativePath 

instead of

window.location.href = relativePath

Thanks again for the quick replies,
Dave

Ariya Hidayat

unread,
Jun 28, 2012, 11:14:43 AM6/28/12
to phan...@googlegroups.com
This might be related to the lack of base URL discussed before. Check
the archive.

Brian Donovan

unread,
Jun 28, 2012, 12:27:09 PM6/28/12
to phan...@googlegroups.com
I'm not sure what previous discussion you mentioned, but I've recently experienced the same thing. I'm doing essentially the same thing that Trindaz is doing with approximately the same result. To work around this I ended up writing my own URL.join() function:

class URL
  @join: (base, url) ->
    if /^https?:/.test url
      # new url is absolute, just use it (e.g. http://www.google.com/)
      #
      #   URL.join 'http://www.google.com/', 'http://foo.com/'
      #   => 'http://foo.com/'
      url
    else if /^\//.test url
      # new url is not absolute, but has absolute path (e.g. /logout)
      # strip out the existing path and use the new one, keeping the domain
      #
      #   URL.join 'http://www.google.com/search', '/help'
      #   => 'http://www.google.com/help'
      #
      base.match(/^(https?:\/\/[^/]+)/)[1] + url
    else if base[base.length-1] is '/'
      # new url is relative and old url ends in a slash, so just append the new one
      #
      #   URL.join 'http://foo.com/abc/def/', 'test/page'
      #
      base + url
    else
      # new url is relative and old url does not end in a slash
      #
      #   URL.join 'http://foo.com/abc/def', 'test/page'
      #   => 'http://foo.com/abc/test/page'
      #
      base.substring(0, base.lastIndexOf('/')) + url

While this works, it's important to stress that setting window.location.href to a relative url works fine in a normal browser. I'm guessing phantomjs is doing something differently around this that causes it to not work in this case. I'm happy to assist debugging this issue if needed.

-bd

James Greene

unread,
Oct 16, 2012, 10:22:26 PM10/16/12
to phan...@googlegroups.com
Seems like it may be related to this open WebKit bug:

~~James
Reply all
Reply to author
Forward
0 new messages