aasd
aasd

Reputation: 129

javascript window.location not working

I am new to javascript, and stuck on this problem. I am trying to have the url query string change based on which checkbox checked. But I seem to have some issue with window.location, Some of the JavaScript is below

  var urltest= window.location.pathname;
  var urltest2 = window.location.host;
  var currentURL  = urltest2 + urltest;
  url = currentURL + "?" + arr_opts.join('&');

  document.getElementById('myurl').innerHTML = url;

  //window.location.href = url;
  window.location = url;

The window.location does not work here, but when I change var currentURL to

var currentURLL = window.location.href;

it is work, but not with

  var urltest= window.location.pathname;
  var urltest2 = window.location.host;
  var currentURL  = urltest2 + urltest;

I need window.location to point page to the currentURL above.

Any help would be appreciated.

Upvotes: 1

Views: 4761

Answers (2)

Paul S.
Paul S.

Reputation: 66334

You're not providing a protocol, so the location.href change is being treated as "go to this relative path from the current location", i.e. on this page

window.location = window.location.host + window.location.pathname;
// takes us to
// http://stackoverflow.com/questions/35254564/javascript-window-location-not-working/stackoverflow.com/questions/35254564/javascript-window-location-not-working/35254601

Do one of the following

  • Provide a protocol so it knows it is an absolute URI,

    window.location = window.location.prototcol + '//' + window.location.host + window.location.pathname + '?foo=bar';
    
  • Tell it to re-use the current protocol but work as an absolute URI

    window.location = '//' + window.location.host + window.location.pathname + '?foo=bar';
    
  • Provide an origin (instead of host)

    window.location = window.location.origin + window.location.pathname + '?foo=bar';
    
  • Tell it to re-use the same origin but work as an absolute path

    window.location = window.location.pathname + '?foo=bar';
    
  • Just update the query

    window.location = '?foo=bar';
    

Always choose the most simple option to make your life easier if you ever need to debug, i.e. if you can assume you will always want the same protocol, host and path, just update the query.


Useful knowledge

Starting a URL with..

  • // means same protocol
  • / means same origin
  • ? means same path
  • # means same query (will not re-load)

Upvotes: 2

Pointy
Pointy

Reputation: 413709

The pathname is going to be something like /questions/35254564/javascript-window-location-not-working, and the host is something like stackoverflow.com. If you put those together with code like yours, you get

stackoverflow.com/questions/35254564/javascript-window-location-not-working

That's clearly not correct; the host looks like part of the pathname. You can use protocol and port if you really feel the need to reconstruct the URL from its constituent parts, but using the href seems simpler if all you want to do is add a query string.

Upvotes: 0

Related Questions