Questions tagged [window.location]

window.location is a property in JavaScript for getting the current page's location.

671 questions
505
votes
7 answers

JavaScript: location.href to open in new window/tab?

I have a JavaScript file from a third party developer. It has a has link which replaces the current page with the target. I want to have this page opened in a new tab. This is what I have so far: if (command == 'lightbox') { …
iamjonesy
  • 24,732
  • 40
  • 139
  • 206
422
votes
18 answers

How to remove the hash from window.location (URL) with JavaScript without page refresh?

I have URL like: http://example.com#something, how do I remove #something, without causing the page to refresh? I attempted the following solution: window.location.hash = ''; However, this doesn't remove the hash symbol # from the URL.
Tom Lehman
  • 85,973
  • 71
  • 200
  • 272
360
votes
13 answers

Detect HTTP or HTTPS then force HTTPS in JavaScript

Is there any way to detect HTTP or HTTPS and then force usage of HTTPS with JavaScript? I have some codes for detecting the HTTP or HTTPS but I can't force it to use https: . I'm using the window.location.protocol property to set whatever the site…
Registered User
  • 8,706
  • 9
  • 32
  • 40
315
votes
18 answers

What's the difference between window.location and document.location?

What's the difference between window.location and document.location? Should both of them reference the same object?
Morgan Cheng
  • 73,950
  • 66
  • 171
  • 230
263
votes
10 answers

JavaScript hard refresh of current page

How can I force the web browser to do a hard refresh of the page via JavaScript? Hard refresh means getting a fresh copy of the page AND refresh all the external resources (images, JavaScript, CSS, etc.).
leepowers
  • 37,828
  • 23
  • 98
  • 129
127
votes
4 answers

Difference between window.location.assign() and window.location.replace()

What is the difference between window.location.assign() and window.location.replace(), when both redirect to a new page?
Bakudan
  • 19,134
  • 9
  • 53
  • 73
87
votes
9 answers

window.location versus just location

Across the web, I see a vast number of JavaScript programmers writing window.location instead of just location. I was curious if anyone could offer an explanation as to why. window is the global object, and therefore it is unnecessary to include --…
Reid
  • 18,959
  • 5
  • 37
  • 37
72
votes
3 answers

How can I make a HTML a href hyperlink open a new window?

This is my code: test When you click it, it takes you to Yahoo but it does not open a new window?
TheBlackBenzKid
  • 26,324
  • 41
  • 139
  • 209
56
votes
5 answers

Change hash without reload in jQuery

I have the following code: $('ul.questions li a').click(function(event) { $('.tab').hide(); $($(this).attr('href')).fadeIn('slow'); event.preventDefault(); window.location.hash = $(this).attr('href'); }); This simply fades a div in…
daveredfern
  • 1,235
  • 4
  • 14
  • 15
54
votes
6 answers

What is the difference between "window.location.href" and "window.location.hash"?

I learned "window.location.hash" new and tried in my jquery code instead of "window.location.href" and both of them gave same results. Code is here : window.location.href = ($(e.currentTarget).attr("href")); window.location.hash =…
kalaba2003
  • 1,231
  • 3
  • 20
  • 33
49
votes
3 answers

Setting JavaScript window.location

I'm currently setting the window.location.pathname property to redirect the user to a relative URL. The new URL has parameters, so the line of JavaScript looks like this: window.location.pathname = window.location.pathname.substring( 0,…
Mark
  • 715
  • 2
  • 7
  • 9
45
votes
11 answers

window.location.href doesn't redirect

I know this is a question much discussed but I can't figure out why it does not work for me. This is my function: function ShowComments(){ alert("fired"); var movieShareId = document.getElementById('movieId'); //alert("found div" +…
user2235124
  • 471
  • 1
  • 4
  • 4
36
votes
1 answer

possible to replace window.location.hash?

I'm wondering whether it's possible to change the hash in window.location.hash and replace it with 'this.id'. Or would I need to change the entire window.location?
circey
  • 2,032
  • 6
  • 35
  • 51
35
votes
4 answers

removing the # from window.location.hash

I have this simple script: $(document).ready(function(){ var $yoyo = window.location.hash; alert($yoyo); }); But I need to get rid of the # symbol as I'll be using the variable to locate div ids. I've tried using .remove('#') but that doesn't…
circey
  • 2,032
  • 6
  • 35
  • 51
32
votes
2 answers

Adding http headers to window.location.href in Angular app

I have a angular app that I needed to redirect outside to a non angular html page, so I thought I could just use the $window.location.hrefto redirect the angular app to my external site. This actually works fine, however, I have a nodejs/express…
britztopher
  • 1,214
  • 2
  • 16
  • 26
1
2 3
44 45